麻烦循环并避免崩溃

时间:2015-07-16 14:28:36

标签: python loops

我一直无法使这个程序循环...我想要我输入如果他们给错误格式的字符串或日期...这是我的代码,我不知道为什么它不起作用。每次我运行它并输入一个字符串,第一次它会说“糟糕!这不是一个有效的日期。再试一次......”如果用户再次输入错误的输入它会崩溃

这是我的代码。

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)

这就是它的错误:

birth_date = datetime.strptime(birthday, '%m/%d/%Y')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'ASFA' does not match format '%m/%d/%Y'
logout

3 个答案:

答案 0 :(得分:2)

好吧,你没有什么可以捕获你的except块中的错误。你可能想要:P

尝试:

while True: #Don't need 1==1, while True works too!
    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..."
        #Because everything else is in an else block, now goes back
        #to start of loop
     else:
        #only happens if no exceptions happen
        if (((datetime.today() - birth_date).days)/365.2425) > 110:        
            print "Sorry You are older than 110 year i cannot do that math."
        #rest of your elif tree goes here, etc, etc.
        else: #I'm valid data! Finally!
            break
birth_date #do your calculations here, outside the loop?

答案 1 :(得分:0)

发生错误是因为raw_input“ASFA”的输入数据与预期的日期时间格式'%m /%d /%Y'匹配

尝试输入类似“01/20/1999”的内容。

答案 2 :(得分:0)

您的代码格式错误:

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)

您的问题很可能是因为您试图在except块内的代码中添加更多错误输入。这个例外未经处理。你不想只在你的内部“再试一次”。您希望它让用户知道出了什么问题,然后回到开头。如果他们在该异常中输入了一些更糟糕的输入则将无法处理。

我从while循环中取出了剩余的代码,因为我认为这是你想要的。你的程序将永远运行其他明智的。如果你想要那么你应该使用@ NightShadeQueen的答案。我假设你只是希望处理异常的部分循环。

让它永远运行(至少直到用户点击 ctrl + c 或者你定义了另一种退出方式)将它包装在另一个{{1}中} loop:

while

虽然更像这样的事情可能更可取:

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))

这可以防止用户表单在点击 ctrl + c 时显示令人讨厌的错误文本。