我想解決這個問題。這是問題和代碼。 #寫一個過程date_converter,它需要兩個輸入。第一個是 #字典,第二個是字符串。該字符串是格式月/日/年的 #中的有效日期。該程序應返回 #表格中寫入的日期。 #例如,如果 #詞典是英文,python密鑰錯誤信息
english = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May",
6:"June", 7:"July", 8:"August", 9:"September",10:"October",
11:"November", 12:"December"}
# then "5/11/2012" should be converted to "11 May 2012".
# If the dictionary is in Swedish
swedish = {1:"januari", 2:"februari", 3:"mars", 4:"april", 5:"maj",
6:"juni", 7:"juli", 8:"augusti", 9:"september",10:"oktober",
11:"november", 12:"december"}
# then "5/11/2012" should be converted to "11 maj 2012".
# Hint: int('12') converts the string '12' to the integer 12.
def date_converter(dic, n):
theSplit = n.split("/")
a = theSplit[0]
b = theSplit[1]
c = theSplit[2]
if a in dic:
return b + " " + dic[theM] + " " + c
else:
return None
print date_converter(english, '5/11/2012')
#>>> 11 May 2012
print date_converter(english, '5/11/12')
#>>> 11 May 12
print date_converter(swedish, '5/11/2012')
#>>> 11 maj 2012
print date_converter(swedish, '12/5/1791')
#>>> 5 december 1791
輸出: 無 無 無 無 註銷
[工藝補]
什麼是問題。
謝謝。我不知道你必須指定它是一個整數。 – user1937034