鱼C论坛

 找回密码
 立即注册
查看: 733|回复: 3

[已解决]代码修改

[复制链接]
发表于 2020-4-6 00:13:34 | 显示全部楼层 |阅读模式

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

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

x
temp = input('请输入一个年份:')
while not temp.isdigit():
    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 + ' 不是闰年!')






以上代码写年份算是不是闰年,是能写一次,如何修改成:如果不是闰年还可以继续写,或者多次写,求教
最佳答案
2020-4-6 00:27:55
本帖最后由 txxcat 于 2020-4-6 00:37 编辑

加一个while循环,但是要设置好退出循环的条件,参考下面代码:
  1. while True:
  2.     print('请输入一个整数(按q退出):',end='')
  3.     temp = input()
  4.     if temp.lower()=='q':
  5.         break
  6.     elif not temp.isdigit():
  7.         print('抱歉,您的输入有误,',end='')
  8.         continue
  9.     year = int(temp)
  10.     if year/400 == int(year/400):
  11.         print(temp + ' 是闰年!')
  12.     else:
  13.         if (year/4 == int(year/4)) and (year/100 != int(year/100)):
  14.             print(temp + ' 是闰年!')
  15.         else:
  16.             print(temp + ' 不是闰年!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-6 00:27:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 txxcat 于 2020-4-6 00:37 编辑

加一个while循环,但是要设置好退出循环的条件,参考下面代码:
  1. while True:
  2.     print('请输入一个整数(按q退出):',end='')
  3.     temp = input()
  4.     if temp.lower()=='q':
  5.         break
  6.     elif not temp.isdigit():
  7.         print('抱歉,您的输入有误,',end='')
  8.         continue
  9.     year = int(temp)
  10.     if year/400 == int(year/400):
  11.         print(temp + ' 是闰年!')
  12.     else:
  13.         if (year/4 == int(year/4)) and (year/100 != int(year/100)):
  14.             print(temp + ' 是闰年!')
  15.         else:
  16.             print(temp + ' 不是闰年!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 08:05:21 | 显示全部楼层
  1. while True:
  2.     temp = input('请输入一个年份:')
  3.     while not temp.isdigit():
  4.         temp = input("抱歉,您的输入有误,请输入一个整数:")

  5.     year = int(temp)
  6.     # 如果是闰年则退出死循环
  7.     if year/400 == int(year/400):
  8.         print(temp + ' 是闰年!')
  9.         break
  10.     else:
  11.         if (year/4 == int(year/4)) and (year/100 != int(year/100)):
  12.             print(temp + ' 是闰年!')
  13.             break
  14.         else:
  15.             print(temp + ' 不是闰年!请再输入一次!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-6 12:48:57 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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