围着鳕鱼转的化合物

2024-04-25 23:50:26 发布

您现在位置:Python中文网/ 问答频道 /正文

忽略!很抱歉,我意识到python将x in range()用于while循环

#Point Calc System

from random import randint
        team1 = 0
        team2 = 0
        winloss = 0
        check = 0
        x = 0
        i = 0

  #while x < 6:  - Didnt work, probably formatting issue -
        #x += 1
        #Runs best of 3 games
        while i < 3:
            i = i + 1

            #2 = win // 1 = loss
            winloss = randint(1,2)
            check = check + winloss
            print("Check = ",check)

            if winloss == 2:
                team1 = team1 + 2
                print("win ",team1)
            else:
                team2 = team2 + 2
                print("loss ",team2)

        #Test to see if a Team won 2/3 battles or won 2 in a row.
        if team1 > team2 and check == 5:
            team1 = team1 + 2
            print("team1 wins",team1)

        elif team1 > team2 and check == 6:
            print("Team1 wins no add",team1)

        elif team2 > team1 and check == 4:
            team2 = team2 + 2
            print("team2 wins",team2)

        elif team2 > team1 and check == 3:
            print("Team2 wins no add",team2)

        else:
            #Should never be seen.
            print("error")            

我原以为在顶部添加另一个while循环会起作用,但事实并非如此,添加了一个简单的:whilex<;6:x+=1。。。没用。你知道吗


Tags: andinifcheckwinelseprintrandint
1条回答
网友
1楼 · 发布于 2024-04-25 23:50:26

很可能是缩进错误。你知道吗

如果在代码前面有空格的终端中运行Python

>>>   x = 1 
File "<stdin>", line 1
    x =5
    ^
IndentationError: unexpected indent

这个执行没有问题

>>>x = 1 

也就是说,在去掉不正确的空格之后,下面的代码执行起来没有任何问题。你知道吗

#Point Calc System

from random import randint
team1 = 0
team2 = 0
winloss = 0
check = 0
x = 0
i = 0
while x < 6:
    x += 1
    #Runs best of 3 games
    while i < 3:
        i = i + 1
        #2 = win // 1 = loss
        winloss = randint(1,2)
        check = check + winloss
        print("Check = ",check)

        if winloss == 2:
            team1 = team1 + 2
            print("win ",team1)
        else:
            team2 = team2 + 2
            print("loss ",team2)

    #Test to see if a Team won 2/3 battles or won 2 in a row.
    if team1 > team2 and check == 5:
        team1 = team1 + 2
        print("team1 wins",team1)

    elif team1 > team2 and check == 6:
        print("Team1 wins no add",team1)

    elif team2 > team1 and check == 4:
        team2 = team2 + 2
        print("team2 wins",team2)

    elif team2 > team1 and check == 3:
        print("Team2 wins no add",team2)

    else:
        #Should never be seen.
        print("error")   

注意:还要记住不要混合制表符和空格(在while循环或if/else语句的缩进中)

相关问题 更多 >