sakanakun 发表于 2020-4-3 15:25:05

能帮忙看看中间的怎么简化吗?

time = 5
while time:
    temp = input('请输入一个年份:')
    if 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 + ' 不是闰年!')
      
    else:
      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 + ' 不是闰年!')
    time = time - 1
print('结束了~')


整数和非整数判定后感觉还是很繁琐,尝试改了几次都不成功{:9_220:}

qiuyouzhi 发表于 2020-4-3 15:29:17

去掉判断还能更少:
n = input("请输入一个年份:")
while not n.isdigit():
    n = input("输入错误,请重新输入:")
n = int(n)
if not n % 400 or (n % 100 and not n % 4):
    print("%d 是闰年!" % n)
else:
    print("%d 不是闰年!" % n)

qiuyouzhi 发表于 2020-4-3 15:30:47

看了看你的代码,为何不用取余呢?
用除法多麻烦,而且就是用除法
你也得用地板除啊

BngThea 发表于 2020-4-3 15:47:51

试试用取余运算,逻辑要简单很多的

dadabiubiubiu 发表于 2020-4-6 15:49:38

while True:
    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 + ' 不是闰年!')
页: [1]
查看完整版本: 能帮忙看看中间的怎么简化吗?