wfjx_85 发表于 2017-12-14 16:54:50

print()格式的再认知

今天做第五讲课后题时犯了一个错误
temp = input('请输入一个年份:')
while not temp.isdigit():
    temp = input('格式错误,请输入一个整数年份:')
year = int(temp)
if year/4 == int(year/4) and year/100 != int(year/100):
    print(‘’year‘’+'是闰年')
elif year/400 == int(year/400):
    print(‘year’+'是闰年')
else:
    print('year'+'不是闰年')

结果就输出了笑话{:10_266:}

请输入一个年份:1900
year不是闰年

后来改成print(year+'是闰年')
直接报错

最终发现,print(),内只能跟字符串。year已经被执行了int()。
正确的方式应该是
print(temp+'是闰年')
页: [1]
查看完整版本: print()格式的再认知