2014-03-03 41 views
-3

我需要添加了用戶已經出生的日期進入不同的值,因此,例如在更短的代碼:添加了不同的值

year = input('Enter the year he/she was born in (yyyy) : ') 
month = input('Enter the month he/she was born in (mm) : ') 
day = input('Enter the day he/she wa born in (dd) : ') 

現在我該怎樣所有這些值加起來把它變成DD/MM/YYYY

可以結束結果被稱爲fulldob請

(這是不是整個代碼,因此並不複雜)

請協助

+1

看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能會收到的答案就越多。 –

+2

親愛的Akshay Patel,你以前的問題的答案發生了什麼事情(如http://stackoverflow.com/questions/22052625/subroutine-not-working-error-message-saying-variable-is-not-defined)。沒有任何幫助你? – Hyperboreus

+0

@Hyperboreus是的,上一個問題的答案有效,但我使用了第一個答案,而不是第二個答案,因爲這對我來說很複雜 – ap306

回答

0

正如你已經回答你的最後一個問題,爲什麼重新發明輪子? Python在標準庫中提供日期/時間操作,所以只需使用它。所以你確定每個月的日子都在範圍之內,閏年需要照顧等等。

from datetime import datetime 

def inputDate(): 
    while True: 
     try: return datetime.strptime(input('Enter date of birth (m/d/yyyy): '), '%m/%d/%Y') 
     except ValueError: pass 

a = inputDate() 
print(a.day, a.month, a.year) 
print(a.strftime('%d/%m/%Y')) #your desired format