在Python3中停止函数内部的循环

在Python3中停止函数内部的循环,python,function,loops,Python,Function,Loops,如何停止while循环?您可以使用break为或while循环中断最近的。在您的情况下,需要在while循环中检查compareInput的返回值,然后根据需要中断或返回 import random def compareInput(n): if n == randomNumber: #Compares guess and the random number print("YAY!! you won the number was: " + n) retu

如何停止while循环?

您可以使用
break
while
循环中断最近的
。在您的情况下,需要在
while
循环中检查
compareInput
的返回值,然后根据需要中断或返回

import random

def compareInput(n):
    if n == randomNumber: #Compares guess and the random number
        print("YAY!! you won the number was: " + n)
        return
        #(something) Loop should stop here.
    else:
        print("Wrong guess")



print("Guess the number") #Prints guess number duh!
randomNumber = str(random.randint(1,20)) #Generates a number between 1 and 20 and converts it to string
while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
    else:
        print("Wrong input")
while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
        break  ## you exit loop with break statement
    else:
        print("Wrong input")

您可以使用
break
循环时中断最近的
。在您的情况下,需要在
while
循环中检查
compareInput
的返回值,然后根据需要中断或返回

while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
        break  ## you exit loop with break statement
    else:
        print("Wrong input")

您可以使用
break
循环时中断最近的
。在您的情况下,需要在
while
循环中检查
compareInput
的返回值,然后根据需要中断或返回

while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
        break  ## you exit loop with break statement
    else:
        print("Wrong input")

您可以使用
break
循环时中断最近的
。在您的情况下,需要在
while
循环中检查
compareInput
的返回值,然后根据需要中断或返回

while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
        break  ## you exit loop with break statement
    else:
        print("Wrong input")


如果猜测不正确,这将始终保持平衡。是的,无论发生什么情况,这都将打破循环。@andJusticeForAll如果输入正确,它将被打破precise@m.wasowski:输入正确与否都会中断,“准确地说”。也许我太累了,但非数字值将如何中断?只需测试它,即使猜测不正确,它也将始终保持平衡。是的,无论发生什么情况,这都将打破循环。@justiceforall它将在正确的输入下中断precise@m.wasowski:输入正确与否都会中断,“准确地说”。也许我太累了,但非数字值将如何中断?只需测试它,即使猜测不正确,它也将始终保持平衡。是的,无论发生什么情况,这都将打破循环。@justiceforall它将在正确的输入下中断precise@m.wasowski:输入正确与否都会中断,“准确地说”。也许我太累了,但非数字值将如何中断?只需测试它,即使猜测不正确,它也将始终保持平衡。是的,无论发生什么情况,这都将打破循环。@justiceforall它将在正确的输入下中断precise@m.wasowski:输入正确与否都会中断,“准确地说”。也许我太累了,但非数字值将如何中断?测试一下谢谢,这就是我要找的谢谢,这就是我要找的谢谢,这就是我要找的谢谢,这就是我要找的谢谢,这就是我要找的好的选择,我也喜欢。好的选择,我也喜欢。好的选择,我也喜欢。好的选择,我也喜欢。好的选择,我也喜欢。