如何阻止整个循环?

时间:2012-04-26 09:43:17

标签: python

我在下面的循环中遇到了python问题。

一旦totalout = 4,它就不会停止,但只有当scorein的整个循环结束时才会停止。 (即第三环)

例如,如果totalout = 4 in scorein number 2,则运行循环直到达到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("")

2 个答案:

答案 0 :(得分:1)

您希望break突破3个循环吗?我想你是从问题的标题来判断的

在这种情况下,由于它是函数的结尾,您只需将break替换为return

答案 1 :(得分:0)

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

while totalscore<numberofscore or totalout<10: