继续做题~经过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。