【发布时间】:2020-11-25 06:48:14
【问题描述】:
我只是尝试编写下面的代码,它只取 1 到 10 之间的整数,并打印一些输入值不满足所需条件的语句。
gamelist = list(range(0,11))
def display_game(gamelist):
print('Here is your list of options:')
print(gamelist)
def user_choice():
choice = 'x'
acceptable_range = range(0,11)
within_range = False
while choice.isdigit() == False or within_range == False:
choice = input('Please enter a digit from the above options:')
if not choice.isdigit():
print("Oops! That's not a digit. Please try again.")
if choice.isdigit():
if int(choice) in acceptable_range:
within_range == True
else:
within_range == False
print('The entered number is out of acceptable range!')
return int(choice)
display_game(gamelist)
user_choice()
在运行此代码后,即使输入了正确的值,它也会继续要求输入。我不明白到底出了什么问题以及哪里出了问题。
【问题讨论】:
-
请做一些在线研究;所以不是这个问题的最佳地点。建议关闭。
-
查看函数的文档。
-
请检查如何提问,做一个简单的谷歌搜索(对于这样的问题),你会发现很多资源。此外,@AnushkaBhakare 搜索源代码和文档。就足够了,一开始您还可以在文本编辑器中使用 KIte 获取更多帮助。
-
从问题中不清楚您是否在询问如何理解函数的源代码以查看它需要哪些参数 - 在这种情况下,请阅读任何基本的 Python 参考资料 - 或如何获得以编程方式来自函数对象的函数签名,在这种情况下,请参阅@Selcuk 链接的答案。
标签: python python-3.x