你是我的小可爱 发表于 2020-2-15 12:58:46

新手编程

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 + ' 不是闰年!')


我想在上面加次数该怎么加,我自己试了半天都没试成功,希望有大佬指点一下!

qiuyouzhi 发表于 2020-2-15 12:59:24

加什么次数

你是我的小可爱 发表于 2020-2-15 13:02:10

qiuyouzhi 发表于 2020-2-15 12:59
加什么次数

就是我打第一个年份他输出不是闰年,然后我可以继续输入年份确定是不是闰年,不用再重新启动这个程序就可以继续操作

qiuyouzhi 发表于 2020-2-15 13:07:37

在外面套一个while循环就好
while True:
    temp = input('请输入一个年份:')
    if temp == 'exit': # 退出条件
      break
    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-2-15 13:12:31

原来也可以这样啊,万分感谢{:5_109:}
页: [1]
查看完整版本: 新手编程