复合循环代码

时间:2016-11-25 16:09:27

标签: python loops while-loop

忽略!我道歉我发现python用于范围()中的x for 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循环会起作用但它没有,添加一个简单的:while x&lt; 6:x + = 1 ......没有用。

1 个答案:

答案 0 :(得分:0)

最有可能的是,你有一个缩进错误。

如果您在代码前面有空格的终端中运行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语句中)