任何人都知道为什么我得到 "Process finished with exit code 0"?
Anyone know why I'm getting a "Process finished with exit code 0"?
我正在尝试 运行 这个程序,但出于某种原因,每当我输入 0 时,程序都会停止 运行ning。我希望 "You've guessed too low, please try again " 因为输入小于生成的随机数。任何人都可以帮助和解释吗?另外,请随时批评我的代码,以便我改进它。非常感谢。
# 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
是错误的,即它在布尔表达式中的计算结果为 False
。因此,以
开头的while
循环
while userGuess:
如果 userGuess
为 0
,将跳过 。看起来您不需要检查循环中的任何条件,因此将其更改为
while True:
应该够了。顺便说一句,Process finished with exit code 0
只是意味着程序退出没有任何错误。
在 python 0 等于 "false"。因此,当您输入“0”时,userGuess 变为假并且 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
我正在尝试 运行 这个程序,但出于某种原因,每当我输入 0 时,程序都会停止 运行ning。我希望 "You've guessed too low, please try again " 因为输入小于生成的随机数。任何人都可以帮助和解释吗?另外,请随时批评我的代码,以便我改进它。非常感谢。
# 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
是错误的,即它在布尔表达式中的计算结果为 False
。因此,以
while
循环
while userGuess:
如果 userGuess
为 0
,将跳过 。看起来您不需要检查循环中的任何条件,因此将其更改为
while True:
应该够了。顺便说一句,Process finished with exit code 0
只是意味着程序退出没有任何错误。
在 python 0 等于 "false"。因此,当您输入“0”时,userGuess 变为假并且 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