|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:1. 在交互模式中,使用 Python 计算一年有多少秒?
这个题严谨来说是不是该分平年闰年?
>>> year=input("请输入年份:")
请输入年份:2008
>>> if year%400==0:
second=366*24*60*60
print("该年有{}秒".format(second))
elif year%4==0:
SyntaxError: unindent does not match any outer indentation level
(这边用判断语句的空格我一直有疑问,试了下不打空格是对的,可是 if 和 elif 不是要同级吗?为啥elif不打空格?)
改正了一下:
>>> year=int(input("请输入年份:"))
请输入年份:2008
>>> if year%400==0:
second=366*24*60*60
print("该年有{}秒".format(second))
elif year%4==0:
second=366*24*60*60
print("该年有{}秒".format(second))
else:
second=365*24*60*60
print("该年有{}秒".format(second))
该年有31622400秒
这个代码我感觉自己写得有点复杂,怎样更简洁一些?
- year = int(input('请输入年份:'))
- if year%400 == 0 or (year%4 == 0 and year%100 != 0):
- i = 366
- else:
- i = 365
- print("该年有{}秒".format(i*86400))
复制代码
|
|