pytho零基础第5讲判断年份闰年为什么这个代码不可以
temp = input('请输入一个年份:')while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if year/400 ==0):
print(temp ' 是闰年!')
else:
if (year/4 == 0) and (year/100 != 0)):
print(temp ' 是闰年!')
else:
print(temp ' 不是闰年!')
year/400 ==0是什么意思?如果是python只有year为0时才成立。 本帖最后由 star58754 于 2018-4-4 16:55 编辑
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if year%400 ==0:
print(temp ,' 是闰年!')
else:
if (year%4 == 0) and (year%100 != 0):
print(temp, ' 是闰年!')
else:
print(temp ,' 不是闰年!')
逻辑我没管, 但是标点符号各种错误, 你对比看一下 while True:
temp = input("請輸入一個年份: ")
if temp == "q": #输入q表示退出
break
if not temp.isdigit():
print(" 輸入錯誤,請重新輸入!!!")
else:
year = int(temp)
# 条件1、能整除4且不能整除100条件2、能整除4, 能整除100, 又能整除400
if year % 4 == 0: # 1和2: 能整除4
if year % 100 == 0: # 2: 能整除100
if year % 400 == 0: # 2: 能整除400
print(str(year) + ("是閏年"))
else:
print(str(year) + ("不是閏年"))
else: # 1: 不能整除100
print(str(year) + ("是閏年"))
else:
print(str(year) + ("不是閏年"))
逻辑不对,应该用取余而不是除法
页:
[1]