![](/img/trans.png)
[英]I want to get an integer input from a user and then make the for loop iterate through that number, and then call a function that many times
[英]how to make next input question repeat a number of times based on user input from previous function
我编写此代码是因为我希望该函数仅在用户输入 1 到 5 时才继续执行下一个:
def max_pizza(question):
error = "Please enter a number between 1 and 5."
valid = False
while not valid:
try:
response=int(input(question))
if 1<= response <=5:
print("-----------------------------------------------------------------")
return response
else:
print(error)
except ValueError:
print(error)
只是我需要关于如何使函数根据最后一个函数的输入重复问题的帮助,因为每次我似乎尝试输入代码并运行它时,它都说函数对象不能解释为整数,并且我不知道该怎么做。
首先,我在示例中没有看到您调用函数max_pizza
。 在代码末尾添加,如果您还没有这样做的话max_pizza("Anything")
。
其次,它对我来说很好用(使用Python 3.8.6
)。 尝试将 python 版本更新到最新版本。 我不确定是否将某些内容更改为input
或int
但这两点是目前唯一的区别。
第三,也许您正在某处调用此函数,那么您也应该发布该函数以及确切的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.