我编写了一些代码,以便人们可以输入日期。停止输入小于 1 或大于 12 的月份的错误检查应仅在其在这些范围内时返回一个值。如果我输入一些“超出范围”的数字,它会正确地重新要求重新输入一个月,但会返回所有值。到底是怎么回事?
# the question asked to get the month input for the xml updater
def month_q():
try:
month = int(input('What was the month [MM] which the installers were updated/created by xxx?:'))
except:
print("That was not a valid number. Please re-enter a 2 digit month")
month_q()
updatemonth = month_check(month)
print("Month q returning:", updatemonth)
return updatemonth
# check the update month is a valid month
def month_check(month):
if month < 1:
print("The month must be a number between 01 and 12. Please re-enter")
month_q()
elif month > 12:
print("The month must be a number between 01 and 12. Please re-enter")
month_q()
else:
print("Month_check returning month:", month)
return month
# this updates the xml file with the date of the last installer
def xml_updater():
updatemonth = month_q()
print("Update month:", updatemonth)
xml_updater()
结果是在输入正确的月份“12”之前输入坏月份“15”、“14”和“13”是:
What was the month [MM] which the installers were updated/created by xxx?:15
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:14
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:13
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:12
Month_check returning month: 12
Month q returning: 12
Month q returning: None
Month q returning: None
Month q returning: None
Update month: None
到底是怎么回事?