Python 故障循环和避免崩溃
我一直很难让这个程序循环。。。我想如果他们以错误的格式给出字符串或日期,我会要求输入…这是我的代码,我不知道为什么它不起作用。每次我运行它并输入字符串时,第一次它会说“哎呀!那不是一个有效的日期。请重试…”如果用户再次输入错误的输入,它将崩溃Python 故障循环和避免崩溃,python,loops,Python,Loops,我一直很难让这个程序循环。。。我想如果他们以错误的格式给出字符串或日期,我会要求输入…这是我的代码,我不知道为什么它不起作用。每次我运行它并输入字符串时,第一次它会说“哎呀!那不是一个有效的日期。请重试…”如果用户再次输入错误的输入,它将崩溃 这是我的密码 while 1 == 1: try: birthday = raw_input("Enter your Birth date in MM/DD/YYYY format: ") birth_date =
这是我的密码
while 1 == 1:
try:
birthday = raw_input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
except ValueError:
print "Oops! That was not a valid date. Try again..."
birthday = raw_input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print "Sorry You are older than 110 year i cannot do that math."
elif ((datetime.today() - birth_date).days) < 0:
print "Sorry you entered a date in the furture."
elif ((datetime.today() - birth_date).days) == 0:
print "OMG You were just born, tomorrow you will be one day old."
else:
print "Age: %d days " % ((datetime.today() - birth_date).days)
发生此错误是因为原始输入“ASFA”中的输入数据与预期的日期时间格式“%m/%d/%Y”不匹配
试着输入“01/20/1999”之类的内容。好吧,除了块之外,你没有什么可以捕捉错误的。你可能想要:P 尝试:
您的代码格式错误:
while True:
try:
birthday = raw_input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
break
except ValueError:
print "Oops! That was not a valid date. Try again..."
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print "Sorry You are older than 110 year i cannot do that math."
elif ((datetime.today() - birth_date).days) < 0:
print "Sorry you entered a date in the furture."
elif ((datetime.today() - birth_date).days) == 0:
print "OMG You were just born, tomorrow you will be one day old."
else:
print "Age: %d days " % ((datetime.today() - birth_date).days)
尽管更像这样的东西可能更可取:
def age_finder():
while True:
try:
birthday = input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
break
except ValueError:
print("Oops! That was not a valid date. Try again...")
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print("Sorry You are older than 110 year i cannot do that math.")
elif ((datetime.today() - birth_date).days) < 0:
print("Sorry you entered a date in the furture.")
elif ((datetime.today() - birth_date).days) == 0:
print("OMG You were just born, tomorrow you will be one day old.")
else:
print("Age: %d days " % ((datetime.today() - birth_date).days))
if __name__ == '__main__':
try:
while True:
age_finder()
except KeyBoardInterrupt:
print()
print('Thanks for using my app')
exit()
def age_finder():
尽管如此:
尝试:
生日=输入(“以MM/DD/YYYY格式输入您的出生日期:”)
出生日期=datetime.strtime(生日,%m/%d/%Y')
打破
除值错误外:
打印(“哦!那不是一个有效的日期。请重试…”)
如果(((datetime.today()-出生日期).days)/365.2425)>110:
打印(“对不起,你已经超过110岁了,我无法计算。”)
elif((datetime.today()-出生日期).days)<0:
打印(“很抱歉,您在以后输入了日期。”)
elif((datetime.today()-出生日期).days)=0:
打印(“天哪,你刚出生,明天你就有一天大了。”)
其他:
打印(“年龄:%d天”((datetime.today()-出生日期).days))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
尝试:
尽管如此:
年龄查找器()
除键盘中断外:
打印()
打印('感谢使用我的应用程序')
退出()
这可以防止用户窗体在按ctrl+c时显示看起来很糟糕的错误文本。是什么输入导致了此错误消息?有关pythonThank的try/except/else/finally结构的更多详细信息,请参阅,这就解决了…但我还有一个问题,如果我想让它继续要求另一个日期,即使他们输入了一个有效的日期,我要求的好奇心,就像我需要有另一个循环
while True:
try:
birthday = raw_input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
break
except ValueError:
print "Oops! That was not a valid date. Try again..."
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print "Sorry You are older than 110 year i cannot do that math."
elif ((datetime.today() - birth_date).days) < 0:
print "Sorry you entered a date in the furture."
elif ((datetime.today() - birth_date).days) == 0:
print "OMG You were just born, tomorrow you will be one day old."
else:
print "Age: %d days " % ((datetime.today() - birth_date).days)
while True:
while True:
try:
birthday = input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
break
except ValueError:
print("Oops! That was not a valid date. Try again...")
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print("Sorry You are older than 110 year i cannot do that math.")
elif ((datetime.today() - birth_date).days) < 0:
print("Sorry you entered a date in the furture.")
elif ((datetime.today() - birth_date).days) == 0:
print("OMG You were just born, tomorrow you will be one day old.")
else:
print("Age: %d days " % ((datetime.today() - birth_date).days))
def age_finder():
while True:
try:
birthday = input("Enter your Birth date in MM/DD/YYYY format: ")
birth_date = datetime.strptime(birthday, '%m/%d/%Y')
break
except ValueError:
print("Oops! That was not a valid date. Try again...")
if (((datetime.today() - birth_date).days)/365.2425) > 110:
print("Sorry You are older than 110 year i cannot do that math.")
elif ((datetime.today() - birth_date).days) < 0:
print("Sorry you entered a date in the furture.")
elif ((datetime.today() - birth_date).days) == 0:
print("OMG You were just born, tomorrow you will be one day old.")
else:
print("Age: %d days " % ((datetime.today() - birth_date).days))
if __name__ == '__main__':
try:
while True:
age_finder()
except KeyBoardInterrupt:
print()
print('Thanks for using my app')
exit()