0

我在下面的python循环中有问题。

它不会在 totalout=4 时立即停止,而是仅在 scorein 的整个循环结束时才停止。(即第三循环)

例如,如果得分 2 中的 totalout=4,它会运行循环直到达到 10

#global value   
totalturn=0
totalscorein=0
totalout=0

def main

  numberofturn=int(input("Number of score:"))

  no_turn=['1','2','3','4','5','6','7','8','9','10'] 
    #while loop condition    
    while totalturn<numberofturn and totalout<10:

            #increasement
            totalscore+=1

            #for loop for score
            for t in range(1,numberofturn+1):
                turns=s*1

                print("\n\n\nThe turn"+no_turn[t]+":",turns)

                #for loop for number to appear from list
                for c in range (10):

                    #list for random number to appear
                    numscore = ['1','2','3','4','5','6','7','8','9','o']

                    #random choice from numscore list to appear
                    from random import choice
                    scorein=choice(numscore)

                    print ("\n\nScores :",scorein)




                    if scorein.isdigit():
                        totalscorein=totalscorein+int(scorein)               


                    if scorein.isalpha():
                        totalout+=1



                    if totalturn==numberofturn:
                        print("\nTotal turn played:",totalturn)


                    elif totalout==4:

                        print("\nTotal turns played",totalturn)
                        break

                    else:

                        print("")
4

2 回答 2

1

你想break打破3个循环吗?我猜你是从问题的标题来看

在这种情况下,由于它是函数的结尾,您可以替换breakreturn

于 2012-04-26T10:13:19.153 回答
0

尝试将and运算符更改为or. 这似乎是你想要的。

while totalscore<numberofscore or totalout<10:

于 2012-04-26T09:59:04.660 回答