2011-03-06 54 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()