“Int”对象不适用于闰年计划

2024-05-19 07:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个程序来计算给定的输入是否是闰年。我正在Windows10上使用Python2.7和AnacondaShell。 这是一个愚蠢的程序,我不明白为什么我得到这个错误。有一对夫妇的解决方案,这个特定的程序在线,但我想让我的版本工作。请帮帮我!你知道吗

我收到错误消息“TypeError:'int'object is not iterable” 我试着在谷歌上搜索一些东西,看看以前的答案,但我很困惑!你知道吗

def sort_year(user_input):
    for i,j in range(0,9):
        if user_input == [ij00]:
            leap_yr1(user_input)
        else:
            leap_yr2(user_input)

def leap_yr1(user_input):
    if user_input % 400 == 0:
        print("The given year is a leap year")
    else:
        print("The given year is not a leap year")

def leap_yr2(user_input):
    if user_input % 4 == 0:
        print("The given year is a leap year")
    else:
        print("The given year is not a leap year")

print("Which year do you want to check?: ")
user_input = int(raw_input())
sort_year(user_input)

Tags: the程序inputifisdef错误not
1条回答
网友
1楼 · 发布于 2024-05-19 07:23:36

我想你应该试着写作

for i in range(0,9):
   for j in range(0,9):

但是我想你应该检查一下user_input中的最后两个数字,这样你就不需要for。你知道吗

如果number结尾有00,那么number % 100 == 0(这里%表示函数modulo

def sort_year(user_input):
    if user_input % 100 == 0:
        leap_yr1(user_input)
    else:
        leap_yr2(user_input)

相关问题 更多 >

    热门问题