|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hello____ 于 2017-7-17 23:08 编辑
temp=input("put a year\n")
while not temp.isdigit:
temp=input("a year!!!\n")
year=int(temp)
print(year)
if (year/400!=int(year/400) )or (year/4!=int(year/4)):
print("no")
else:
print("yes")
lee@lee-DESKTOP:~$ python3 year.py
put a year
400.0
Traceback (most recent call last):
File "year.py", line 6, in <module>
year=int(temp)
ValueError: invalid literal for int() with base 10: '400.0'
为什么
下面代码中的 if temp.isdigit() == False: 就是用来判断输入是否为数字。
isdigit() 如果字符串只包含数字则返回 True,否则返回 False。
- times = 10
- while times > 0:
- times = times - 1
- print('请输入年份',end='')
- temp = input()
- if temp.isdigit() == False:
- print('请输入数字:',end='')
- else:
- year = int(temp)
- if year % 4 == 0:
- if year % 100 == 0:
- if year % 400 == 0:
- print(year,'是闰年')
- else:
- print(year,'不是闰年')
- else:
- print(year,'是闰年')
- else:
- print(year,'不是闰年')
- print('程序结束')
复制代码
|
|