![](/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.