先上问题:input BIF
看下面这段代码,无论怎么测试都退不出循环,很郁闷
print('=-------------闰年计算器-------------=')
print('注意:输入查询的年份,格式:XXXX,如2014')
temp = 1
while temp != 110:
temp = input('请输入查询的年份: ')
while temp.isdigit() != 1:
temp = input('对不起,输入格式为正数!重新输入:')
year = int(temp)
if year / 400 == int(year / 400):
print('公元', temp, '年是闰年!')
else:
if (year / 4 == int(year / 4)) and (year / 100 == int(year / 100)):
print('公元', temp, '年是闰年!')
else:
print('公元', temp, '年是平年!')
print('GameOver')
为了方便测试,简化一下代码
temp = 0
while temp != 520:
temp = input('enter a number:')
print(temp)
结果是这样的:
enter a number:520
520
enter a number:520
520
enter a number:520
520
enter a number:
想办法解决,查看 while 和 input 两个BIF
>>> help(input)
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.The prompt string, if given, is printed to standard output without a trailing newline before reading input.
哎呀我去,input-read a string from standard input
update code
temp = 0
while temp != '520':
temp = input('enter a number:')
print(temp)
print('=-------------闰年计算器-------------=')
print('注意:输入查询的年份,格式:XXXX,如2014')
temp = 1
while temp != '110':
temp = input('请输入查询的年份: ')
while temp.isdigit() != 1:
temp = input('对不起,输入格式为正数!重新输入:')
year = int(temp)
if year / 400 == int(year / 400):
print('公元', temp, '年是闰年!')
else:
if (year / 4 == int(year / 4)) and (year / 100 == int(year / 100)):
print('公元', temp, '年是闰年!')
else:
print('公元', temp, '年是平年!')
print('GameOver')
总结:BIF使用时,一定要注意