python3練習100題——004


繼續做題~經過python3的測試

原題鏈接:http://www.runoob.com/python/python-exercise-example4.html

題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

我的代碼:

year=int(input("please input the year:"))
month=int(input("please input the month:"))
day=int(input("please input the day:"))          
Month=[31,28,31,30,31,30,31,31,30,31,30,31]           #用一個list存儲每個月的天數
i=0
total=day
while i<month-1:                            #用循環加上之前每個月天數
    total +=Month[i]
    i+=1
if year%400==0 or (year%4==0 and year%400 != 0):     #判斷閏年,跟閏年的定義有關 if month>2:
        total+=1
print("It is the %dth day." %total)

 

思考:輸入年月日只能分開嗎?不能以列表形式輸入嗎?

 

可以先定義一個list,用for循環加上append輸入:

date = []
print("please input year month and day:")
for i in range(3):
    date.append(eval(input()))      #eval也可以換為int

這樣要輸入回車x3次。

 

也可以用split函數加上list函數一次輸入:

date=list(input("please input year,month,day:").split(","))

但是這樣在date列表中的元素都是str,之后使用要轉為int。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM