2017-02-27 1579 views
0

我已經查看了所有堆棧溢出,所以我真的需要幫助。我正在制定一個程序,通過詢問他們的出生年份來計算一個人的年齡,驗證他們的出生年份是正確的,如整數,而不是單詞或任何其他無效類型的答案。然後減去當年的出生年份。我在輸入當前年份並從中減去用戶輸入的出生年份方面遇到了問題。通過計算當年 - 出生年份使用python創建年齡計算器

# Function Boolean is_valid_integer(String input_string) 
    # Declare Boolean is_valid 
    # 
    # is_valid = is input_string a valid integer? 
    # Return is_valid 
    # End Function 

    def is_valid_integer(input_string): 
     try: 
      val = int(input_string) 
      is_valid = True 
     except ValueError: 
      is_valid = False 
     return is_valid 

    # Function Integer get_year_born() 
    # Declare Boolean is_valid 
    # 
    # Display "What year were you born in? " 
    # Input input_string 
    # Set is_valid = is_valid_integer(input_string) 
    # While Not is_valid 
    #  Display "Please only enter whole years." 
    #  Input input_string 
    #  is_valid = is_valid_integer(input_string) 
    # End While 
    # input_integer = int(input_string) 
    # Return input_integer 
    # End Function 

    def get_year_born(): 
     input_string = input("What year were you born in? ") 
     is_valid = is_valid_integer(input_string) 
     while not is_valid: 
      input_string = input("Please only enter whole years. ") 
      is_valid = is_valid_integer(input_string) 
     input_integer = int(input_string) 
     return input_integer 

    # Function Integer calculate_difference() 
    # difference = 2017 - input_integer 
    # End Function 

    import datetime 
    def calculate_difference(difference): 
     difference = 2017 - input_integer 
     return difference 



    # Module calculate_age_year() 
    # Set born = get_year_born() 
    # Call calculate_difference() 
    # End Module 

    def calculate_difference(): 
     print("Your age is: ", difference) 

    calculate_age_year() 

嘗試將datatime導入到我的編碼中後,效果不佳。我也不想計算具體的日期和/或時間,所以我刪除了那些建議編碼的部分,可能與它有關係?

我對這一計劃的目的是真的纔算年,所以如果我出生在2000年,我希望該計劃從2017年計算,這意味着我會是17歲截至目前。

我的第一個功能是使虛假輸入無效的循環,第二個功能是獲取用戶出生的年份,第三個功能應該是計算當前日期和用戶出生日期之間的差異,以及輸出用戶的實際年齡的第四功能。

+0

在你的get_number_of_age()方法中,你沒有做任何減法。 age = current_year - input_integer ...你所做的只是返回用戶輸入。 – reticentroot

+0

聽起來像功課?分解成步驟1)獲得年齡。 2)驗證年齡3)用它做一些事情。首先關注各個步驟。 – user1269942

+0

顯示你的實際嘗試。 「當我實際上輸入某些東西時......」並沒有向我們展示你實際上在做什麼,所以我們不能告訴你如何解決它。就目前而言,'today'沒有定義,因此調用「calculate_age_year()」時代碼崩潰。 –

回答

0

要在Python中的日期時,您可以使用該模塊datetime

爲了驗證錯誤,做的Python的方式是使用try...catch用戶輸入轉換爲日期時間。抓住可能的錯誤,並從那裏做錯誤管理。

下面是一個簡單的例子:

import datetime 


def print_age(): 
    year = raw_input('Enter your age (YYYY/MM/DD): ') 

    try: 
     year = [int(i) for i in year.split('/')] 
     birth_data = datetime.date(year[0], year[1], year[2]) 

     today = datetime.date.today() 
     time_diff = today - birth_data 

     print('Your age is: {} year and {} days'. format(
      time_diff.days // 365, time_diff.days % 365)) 
    except ValueError, KeyError: 
     # Do your error management here 
     print('Please use the format YYYY/MM/DD') 


print_age() 

有了這個,應該是比較簡單的返回TrueFalse,並把功能在一個循環。

編輯

下面是一段代碼,詢問他的出生年份的用戶和打印的區別:

import datetime 

year = None 
while year is None: 
    try: 
     user_input = raw_input('Enter your date of birth (YYYY): ') 
     year = int(user_input) 
    except ValueError: 
     print('Your did not enter a valid year!') 

print('You were born {} years ago'.format(datetime.datetime.now().year - year)) 

它不檢查爲今年的有效性,雖然,如果你輸入61241,它不會返回錯誤。

如果您願意,您可以添加一個驗證層來檢查一年的範圍。

+0

謝謝你的幫助,但是我不希望程序計算特定的日期或時間,僅僅是一年。你能幫我多一點嗎?我已經試過在你提到它之前和之後導入日期時間,但我覺得我可能會使用它錯誤,所以它不工作?我已經按照我認爲它會工作的方式添加了它(我嘗試的最後一種方法是我發佈的最新方式),並且它不起作用。任何建議將非常有幫助! – andyhle95

+0

你可以檢查編輯,它應該更接近你想要實現的。 –

+0

目前仍在努力,感謝您的幫助。我的任務是選擇任何一種程序來真正編碼,我選擇編寫一個計算器,通過計算出生年份(輸入),並從當前年份(2017年) 。不幸的是,一切都必須有它自己的塊,輸入,輸出等...這是我現在的代碼,如果你可以看一看,讓我知道嗎? – andyhle95