-1
我正在創建一個程序,使用戶輸入一個數字許多次,直到所述數字在100到200之間。如果用戶輸入字符串或非整數,則會創建錯誤消息並系統會提示用戶再次嘗試。目前,我有線路問題:輸入必須在100到200之間python
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()
錯誤實際上是「gue *** sss *** esTakenInt」未定義。數一數s。 – JJJ