我正在创建一个程序,让用户多次输入一个数字,直到所述数字介于100和200之间。如果用户输入字符串或非整数,则会创建错误消息并提示用户再次尝试。目前我遇到了问题:
print("You tried " + guesssesTakenInt + "numbers and " + guessesTakenStr +
"non-number to get it right.")
我假设我必须将它变成一个while循环,以便出现以下错误:guessesTakenInt未定义。
##
## a6p2.py
##
def main():
# this is the code that runs when your program is executed
guessesTakenInt = 0
guessesTakenStr = 0
while True:
guessesTakenStr += 1
try:
userNumber = int(input("Please enter a number between 100 and 200: "))
except ValueError:
print("Sorry that is not a number. ")
continue
else:
break
while userNumber > 0:
guessesTakenInt += 1ed
if userNumber > 200:
userNumber = int(input("This number is too big. Try again: "))
elif userNumber < 100:
userNumber = int(input("This number is too low. Try again: "))
else:
guessesTakenInt = str(guessesTakenInt)
guessesTakenStr = str(guessesTakenStr)
print("That number is just right!")
print("You tried " + guesssesTakenInt + "numbers and " + guessesTakenStr + " non-number to get it right.")
break
return
print (main())
###############################################################
## DO NOT CHANGE THIS! LEAVE IT AT THE BOTTOM OF YOUR FILE
## calls the main function when this file is run with Python
##
## python stats.py
##
###############################################################
if __name__ == "__main__":
main()