【发布时间】:2023-02-19 20:10:24
【问题描述】:
所以我遇到了一个问题,我无法弄清楚如何结束特定的 while 循环。
one = [1, 3, 5, 7, 8, 10, 12]
thirty = [4, 6, 9, 11]
while True:
try:
month = int(input("Enter the number of the month: "))
except month == "":
print("Program ending")
break
except ValueError:
print("Please enter a number")
continue
else:
def days(month):
if month in one:
return 31
elif month in thirty:
return 30
elif month == 2:
return 28
if days(month) == None:
print("The number has to be between 1-12")
else:
print("This month has", days(month) ,"days.")
因此,该程序应该告诉您指定月份有多少天,而我几乎想要实现的是循环继续并再次询问问题,直到用户留下空白输入(按回车键)。我已经在谷歌上搜索了很长一段时间,但似乎无法找到我的确切问题的解决方案。我发现你不能把某些东西的价值放在除了:如果值是在尝试:,目前代码中就是这种情况。 提前致谢 :)
【问题讨论】:
-
您真的打算在 while 循环中定义一个函数吗?并且没有调用该函数,它什么也不做。
-
好吧,老实说,我不知道,我真的是编程新手,除了我无法结束循环之外,一切似乎都正常。如果您有任何更好的建议,那么我愿意接受 :)
标签: python