|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么一直有报错,检查了几次没找到原因,各位大佬帮忙解答下
n = 3
while n > 0 :
year = input('请输入一个年份:')
while year.isdigit():
year = int(year)
if year % 400 == 0:
print("今年为世纪闰年哦!")
else:
if year % 4 == 0:
print("今年为普通闰年!")
else :
print("不好意思","今年不是闰年",sep = "-",end = "!")
while not year.isdigit():
year = input("抱歉,您的输入有误,请输入一个整数:")
n = n - 1
.isdigit() 字符串方法是判断字符串是否为纯数字字符串,而你在内部的循环中 year = int(year)
将之前的字符串 year 变量 覆盖为了整形的 year,所以导致后续调用 .isdigit() 报错
而且你内部的 while 循环是死循环,可以将代码改成这样,内嵌的第二个 while 提前即可,参考代码:
- n = 3
- while n > 0:
- temp = input('请输入一个年份:')
- while not temp.isdigit():
- temp = input("抱歉,您的输入有误,请输入一个整数:")
-
- year = int(temp)
- if year % 400 == 0:
- print("今年为世纪闰年哦!")
- else:
- if year % 4 == 0:
- print("今年为普通闰年!")
- else:
- print("不好意思", "今年不是闰年", sep="-", end="!")
-
- n = n - 1
复制代码
|
|