c=input("請輸入年/月/日:")
n=c.find("/")
l=c[n+4:]
g=c[5:7]
f=c[:n]
month = ("Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.")
c=l+"/"+g+"/"+f
print(c)
要怎麼讓g(自行輸入的月份)被字串month對應的取代
程式超級小白求解
def get_month(month_int):
month = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
return month[month_int-1]
c=input("請輸入年/月/日:")
p = c.split("/")
print(p[0] + "/" + get_month(int(p[1])) + "/" + p[2])
Python初學者來練功囉!!
像這種日期格式轉換的,我一向不自己寫,儘量用現成的....
#iT邦幫忙練功
#https://ithelp.ithome.com.tw/questions/10212374
#原PO程式碼
c=input("請輸入年/月/日:")
n=c.find("/")
l=c[n+4:]
g=c[5:7]
f=c[:n]
#我的程式碼
import datetime
dt1 = datetime.date(year = int(f), month = int(g), day=int(l))
c = dt1.strftime("%Y/%b/%d")
print(c)
執行結果: