我的代码没有做我想要它做的事情,我无法让它退出 while 循环。请解释为什么会这样

  • 本文关键字:解释 循环 while 我想要 代码 退出 python
  • 更新时间 :
  • 英文 :

val = [*range(1,51)]
print("Now, I need aaato know how many state Capitals you would like to practice")
user = input("chose a number from 1 to 50")
while user not in val:
print("There are 50 States in the United States. You need to pick a number between 1-50. If you want to exit the game, type "EXIT"")
user = input("I needbbb to know how many state Capitals you would like to practice")
if user.capitalize() == "EXIT":
break
if user == 0:
print("There are more than zero States in the United Sts That means that you do not want to play today")
user = input("I needccc to know how many state Capitals you would like to practice. If you want to exit the game, type "EXIT"")
print("Hello")

输出:

美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想在多少个州的首府实习美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想在多少个州的首府实习美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想练习多少个州的首府美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想在多少个州的首府实习美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想在多少个州的首府实习美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想练习多少个州的首府美国有50个州。你需要在1-50之间选一个数字。如果你想退出游戏,输入&;exit &;我需要知道你想练习多少个州的首府

我创建了一个从1到50的整数列表。我希望用户从列表(val)中选择一个数字。如果没有,我希望用户继续尝试。除非用户想用"退出"退出。

它只是一直卡在我的用户输入打印语句,我不明白为什么?

您想要user.upper(),而不是user.capitalize()

从help-text:

>>> help(str.capitalize)
Help on method_descriptor:
capitalize(self, /)
Return a capitalized version of the string.
More specifically, make the first character have upper case and the rest lower
case.
>>> help(str.upper)
Help on method_descriptor:
upper(self, /)
Return a copy of the string converted to uppercase.

最新更新