temp = input('请输入年份:')

while not isinstance(temp, int):   temp = input('抱歉,您的输入有误,请输入年份:') year = int(temp) if year % 400 == 0:   print('%d是闰年!'%(year)) else: if year % 4 == 0 and year % 100 != 0:   print('%d是闰年!' %(year)) else:   print('%d不是闰年!' %(year))

python教程第5讲:解决闰年问题----input()的返回值始终是字符串

python教程第5讲:解决闰年问题----input()的返回值始终是字符串

python教程第5讲:解决闰年问题----input()的返回值始终是字符串

python教程第5讲:解决闰年问题----input()的返回值始终是字符串

所以更改后的代码如下(只修改while语句部分):

while not temp.isdigit():   temp = input('抱歉,您的输入有误,请输入年份:')