2015-04-19 71 views
0

標題中的問題。我正在編寫一個程序,要求用戶以mm/dd/yy格式輸入日期,如果日期僅在2013年,請驗證該日期,然後以此格式打印日期:2013年7月1日。 「M剛剛在一個邏輯錯誤的程序仍然會打印出日期,即使一年不2013年。這裏是我的代碼:如何在Python中設置最小和最大日期以驗證輸入?

import datetime 


def main(): 
    def date_method(user_date): 
     date_list = user_date.split('/') 
     month = int(date_list[0]) 
     day = int(date_list[1]) 
     year = 2000 + int(date_list[2]) 
     try: 
      datetime.datetime.min = 1/1/2013 
      datetime.datetime.max = 12/31/2013 
      the_date = datetime.date(month, day, year) 
      correct_date = "True" 
      return the_date, correct_date 
     except ValueError as err: 
      print(err) 
      the_date = 1/1/1 
      correct_date = "False" 
      return the_date, correct_date 

    correct_date = "False" 
    while correct_date == "False": 
     user_date = str(input("Please enter a date in the month/day/year format: mm/dd/yy. ")) 
     the_date, correct_date = date_method(user_date) 
     if correct_date == "True": 
      print("Date: ", datetime.date.strftime(the_date, "%B %d, %y")) 

main() 

當我覺得我離我datetime.datetime.min和datetime.datetime.max變量。

+0

您是否嘗試添加年份變量的if/else語句?這似乎更簡單。 – macmania314

+2

你知道'/'是用來劃分的嗎? –

+0

是的,我現在意識到這一點。我的錯。 – Jasonca1

回答

0

的Python 內置類型不變,所以你不能設置任何屬性,如果你想驗證這一年正好轉換爲datetime對象並檢查.year屬性是>= 13

if mn_year <= dt.year <= mx_year 

即使你可以1/1/2013是一個方程而不是日期。

類似以下內容:

user_date = input("Please enter a date in the month/day/year format: mm/dd/yy. ") 

def date_method(user_date): 
    try: 
     mn, dy, year = user_date.split('/') 
     the_date = datetime.datetime.strptime("{} {} {}".format(mn, dy, year), "%d %m %y") 
     if the_date.year >= 2013: 
      correct_date = "True" 
      return the_date.year, correct_date 
    except ValueError as err: 
     print(err) 
     .... 

如果你想有一個最小和最大使用mn_year和如上mx_year。你不需要投入整數,你可以像使用字符串一樣使用datetime.strptime。您也不需要將輸入投入到str,這正是它的原因。當用戶輸入不正確的數據時,在嘗試中將user_date字符串拆分並解壓縮也會被捕獲。

0

設置datetime.datetime.mindatetime.datetime.max聞起來像全局變量給我。更好的解決辦法是簡單地檢查它是否在一個範圍內。

import datetime 

def date_method(user_date): 
    date_list = user_date.split('/') 
    month = int(date_list[0]) 
    day = int(date_list[1]) 
    year = 2000 + int(date_list[2]) 
    the_date = datetime.date(year, month, day) 
    if datetime.date(2013, 1, 1) <= the_date <= datetime.date(2013, 12, 31): 
     return the_date, True 
    else: 
     print("Date entered is not in the year 2013.") 
     return datetime.date(1, 1, 1), False 

def main(): 
    correct_date = False 
    while correct_date == False: 
     user_date = input("Please enter a date in the month/day/year format: mm/dd/yy. ") 
     the_date, correct_date = date_method(user_date) 
     if correct_date: 
      print("Date: ", datetime.date.strftime(the_date, "%B %d, %y")) 

main() 

代碼中的一些更多的故障:

  • 您使用字符串"True""False",而不是僅僅TrueFalse
  • 它的datetime.date(year, month, day)代替datetime.date(month, day, year)
  • 爲什麼你會定義date_method裏面main()
  • 運營商/不能用於您的示例中的日期。你真正在做的是簡單地分割一個數字,Python將這個數字解析爲一個日期。 (這一點也不像你的意思日期)
+0

我現在明白了。我想保留try/exception子句的一個原因是,我可以打印ValueError異常,告訴用戶他們的輸入有什麼特別的錯誤。如果年份值不是2013年,我將如何能夠打印出錯誤消息?我知道我會在except子句中使用if語句,但我不知道如何從user_date變量訪問年份值。 – Jasonca1

+0

我編輯了我的答案,這是你在追求什麼?我只在else部分添加了一個簡單的'print'語句。 – evertheylen

相關問題