2011-03-06 32 views
1

在下面的代码中,我试图获得第二个'while'语句(while digit_check)从早先的while_count语句接收新日期。但它似乎从第一个赋值行中为user_date变量提取了原始赋值。如何获得正确的变量参考

我怎样才能得到新的变量赋值传递给第二个while语句?

感谢很多

def main(): 

    user_date = raw_input("Enter a date in the format mm/dd/yyyy and press 'Enter' ") 
    count = len(user_date) 
    digit = '' 
    digit_check = '' 

    while count != 10: 
     user_date = raw_input('try again ') 
     count = len(user_date) 

    if user_date[0].isdigit() and user_date[1].isdigit() and user_date[3].isdigit() \ 
     and user_date[4].isdigit() and user_date[6].isdigit() and user_date[7].isdigit() \ 
     and user_date[8].isdigit() and user_date[9].isdigit() and user_date[2] == '/' \ 
     and user_date[5] == '/': 
      digit_check = True 

    while digit_check != True : 
     user_date = raw_input('Not right - try again') 

    convert_date(user_date) 

    print 'That date is ',convert_date(user_date) + ' ' + user_date[3] + user_date[4] + ',' + user_date[6:] 

def convert_date(user_date): 

    # Convert date to different format 
    month = '' 

    if user_date[0] == '0' and user_date[1] == '1': 
     month = 'January' 
    elif user_date[0] == '0' and user_date[1] == '2': 
     month = 'February' 
    elif user_date[0] == '0' and user_date[1] == '3': 
     month = 'March' 
    elif user_date[0] == '0' and user_date[1] == '4': 
     month = 'April' 
    elif user_date[0] == '0' and user_date[1] == '5': 
     month = 'May' 
    elif user_date[0] == '0' and user_date[1] == '6': 
     month = 'June' 
    elif user_date[0] == '0' and user_date[1] == '7': 
     month = 'July' 
    elif user_date[0] == '0' and user_date[1] == '8': 
     month = 'August' 
    elif user_date[0] == '0' and user_date[1] == '9': 
     month = 'September' 
    elif user_date[0] == '1' and user_date[1] == '0': 
     month = 'October' 
    elif user_date[0] == '1' and user_date[1] == '1': 
     month = 'November' 
    elif user_date[0] == '1' and user_date[1] == '2': 
     month = 'December' 

    return month 


main() 
+0

那么,这可能是因为你的巨人如果陈述失败?注意:你应该重写你的代码。 – katrielalex 2011-03-06 23:06:19

+0

@katrielalex,是啊我应该 - 这就是为什么我试图得到一些帮助。也许你应该尝试建设性和有益的! – Paul 2011-03-06 23:36:45

+0

我只是说,如果你证明你已经尝试了一些明显的事情,那么你会更容易获得帮助。在这种情况下,您应该进行一些调试,可能会使用打印语句来查看问题出在哪里。 – katrielalex 2011-03-06 23:50:04

回答

1

的一个问题是,你是不是重新计算digit_check这里:

while digit_check != True : 
    user_date = raw_input('Not right - try again') 

这将只是进入一个无限循环。

我会建议,而不是写一个巨大的函数与许多循环和大量的任务,而是你的代码重构成更小的功能,并使用更简单的逻辑。例如:

def getDate(): 
    while True: 
     user_date = raw_input('Enter a date') 
     if validate(user_date): 
      return user_date 
     else: 
      print 'Error, try again.' 

def validate(user_date): 
    # etc... 
0

您不是在while循环中重新计算digit_check,因此即使user_date正在更改,它也会永远失败。在user_date = raw_input('不正确 - 再试一次')之后,在while循环内添加if块会修复它。

这就是说,你应该明确地检查一个单独的函数,特别是因为你现在将它调用两次而不是一次。

1

或者使用内置的库函数?

import re 
import datetime 

def getDate(msg="Enter a date in the format mm/dd/yyyy and press 'Enter': ", pat=re.compile(r'(\d{1,2})/(\d{1,2})/(\d{4})$')): 
    while True: # repeat until a valid date is entered 
     s = raw_input(msg)   # get input 
     match = pat.match(s.strip()) # match against regular expression 
     if match:      # match found? 
      m,d,y = match.groups() 
      try: 
       # parse to date 
       return datetime.date(int(y), int(m), int(d)) 
      except ValueError: 
       # parsing failed (month 93 is invalid, etc) 
       pass 

def main(): 
    userDate = getDate() 
    print('You entered {0}'.format(userDate.strftime('%B %d, %Y'))) 

if __name__=="__main__": 
    main()