|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
times = 3
years = random.randint(0,3000)
print('请输入一个年份看看他是不是闰年:',end='')
while years.isdigit():
temp = input()
years = int(temp)
times = times - 1
if years/400 == int(years/400):
print(temp + '年是闰年')
else:
if (years/4 == int(years/4)) and (years/100 != int(years/100)):
print(temp + '年是闰年')
else:
print(temp + '年不是闰年')
print('再试一次吧:',end='')
print('谢谢使用')
实际上你这里的 random 模块是没有用处的,因为你是判断你自己输入的数值是不是闰年和随不随机无关。
你代码中的:years.isdigit() 应该改成 temp.isdigit()
years = random.randint(0,3000) 是随机一个整数数值,也就是说是整型,而不能用字符串的 isdigit() 方法,否则会报错
参考代码:
- times = 3
- print('请输入一个年份看看他是不是闰年:',end='')
- while times:
- temp = input()
- while not temp.isdigit():
- temp = input('输入错误,请输入规范的年份:')
- years = int(temp)
- times = times - 1
- if years/400 == int(years/400):
- print(temp + '年是闰年')
- else:
- if (years/4 == int(years/4)) and (years/100 != int(years/100)):
- print(temp + '年是闰年')
- else:
- print(temp + '年不是闰年')
- if times != 0:
- print('再试一次吧:',end='')
- print('谢谢使用')
复制代码
|
|