鱼C论坛

 找回密码
 立即注册
查看: 4425|回复: 4

[已解决]pytho零基础第5讲判断年份闰年为什么这个代码不可以

[复制链接]
发表于 2018-4-4 16:42:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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 ' 不是闰年!')
最佳答案
2018-4-4 16:53:33
本帖最后由 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 ,' 不是闰年!')

逻辑我没管, 但是标点符号各种错误, 你对比看一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-4 16:44:47 | 显示全部楼层
year/400 ==0是什么意思?如果是python只有year为0时才成立。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-4 16:53:33 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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 ,' 不是闰年!')

逻辑我没管, 但是标点符号各种错误, 你对比看一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-4 16:56:35 | 显示全部楼层
  1. while True:
  2.     temp = input("請輸入一個年份: ")
  3.     if temp == "q": #输入q表示退出
  4.         break
  5.    
  6.     if not temp.isdigit():
  7.         print("       輸入錯誤,請重新輸入!!!")
  8.     else:   
  9.         year = int(temp)

  10.         # 条件1、能整除4且不能整除100  条件2、能整除4, 能整除100, 又能整除400
  11.         if year % 4 == 0: # 1和2: 能整除4
  12.             if year % 100 == 0: # 2: 能整除100
  13.                 if year % 400 == 0: # 2: 能整除400
  14.                     print(str(year) + ("是閏年"))
  15.                 else:
  16.                     print(str(year) + ("不是閏年"))
  17.             else: # 1: 不能整除100
  18.                 print(str(year) + ("是閏年"))
  19.         else:
  20.             print(str(year) + ("不是閏年"))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-4 17:30:32 From FishC Mobile | 显示全部楼层
逻辑不对,应该用取余而不是除法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 18:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表