![](/img/trans.png)
[英]I keep getting Process finished with exit code 245 and flask crashes
[英]Anyone know why I'm getting a “Process finished with exit code 0”?
我正在嘗試運行該程序,但是由於某種原因,每當我輸入0時,程序就會停止運行。 我希望輸入的數據少於生成的隨機數,因此您會認為“您猜得太低,請重試”。 誰能幫助和解釋? 另外,請隨時批評我的代碼,以便我可以做得更好。 非常感激。
# Generate random number for player to guess.
import random
number = random.randint(1, 3)
print(number)
# Ask player's name and have player guess the number.
welcomeUser = input("Hi, welcome to 'Guess a Number!'. Please tell us your name ")
userName = str(welcomeUser)
userGuess = int((input("Guess a number from 1 - 3 ")))
# Cycle through player's guesses until player enters correct number.
while userGuess:
if userGuess > number:
print("You've guess too high, please try again ")
userGuess = int(input("Guess a number from 1 - 3 "))
if userGuess == number:
print("Congratulations! You've guessed correctly! ")
break
elif userGuess < number or userGuess == 0:
print("You've guessed too low, please try again ")
userGuess = int(input("Guess a number from 1 - 3 "))
if userGuess == number:
print("Congratulations! You've guessed correctly! ")
break
else:
print("Congratulations " + userName + "! " + "You've guessed correctly! ")
break
0
為falsey,即在布爾表達式中其值為False
。 因此, while
循環從
while userGuess:
如果userGuess
為0
將被跳過。 看起來您不需要檢查循環中的任何條件,因此將其更改為
while True:
應該足夠了。 順便說一句, Process finished with exit code 0
僅表示程序已退出,沒有任何錯誤。
在python 0等於“ false”。 因此,當您輸入“ 0”時,userGuess變為false,而while循環終止。 更好地引入一個新變量:
continueProgram = True
while continueProgram :
if userGuess > number:
print("You've guess too high, please try again ")
userGuess = int(input("Guess a number from 1 - 3 "))
if userGuess == number:
print("Congratulations! You've guessed correctly! ")
continueProgram = False
elif userGuess < number or userGuess == 0:
print("You've guessed too low, please try again ")
userGuess = int(input("Guess a number from 1 - 3 "))
if userGuess == number:
print("Congratulations! You've guessed correctly! ")
continueProgram = False
else:
print("Congratulations " + userName + "! " + "You've guessed correctly! ")
continueProgram = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.