python忽略拼写,即使它是正确的

时间:2018-03-05 10:28:25

标签: python

我的python代码似乎忽略了拼写并说拼写错误。

我尝试过移动代码 更改输入类型,这修复了一个输入错误,但开始出现这个新错误。

input("enter month here. spelling and capitalization matter.")
if input == "December" or input == "January" or input == "February":
    print(input, "is in Winter")
elif input == "March" or input == "April" or input == "May":
    print(input, "is in Spring")
elif input == "June" or input == "July" or input == "August":
    print(input, "is in Summer")
elif input == "September" or input == "October" or input == "November":
    print(input, "is in Autumn")
else:
    print("Check spelling")

4 个答案:

答案 0 :(得分:2)

你可能想这样做:

month = input("enter month here. spelling and capitalization matter.")
if month == "December" or month == "January" or month == "February":
    print(month, "is in Winter")
elif month == "March" or month == "April" or month == "May":
    print(month, "is in Spring")
elif month == "June" or month == "July" or month == "August":
    print(month, "is in Summer")
elif month == "September" or month == "October" or month == "November":
    print(month, "is in Autumn")
else:
    print("Check spelling")
input("Press ENTER to quit")

您缺少的是您没有为关键字input分配变量。

答案 1 :(得分:1)

这是针对您的问题略有不同的方法。您的原始帖子中存在一些语法错误,这可能有助于使代码更易于阅读(更容易发现错误)。

# You can use a dictionary to 'map'
# months to seasons.
seasons = {
    'january': 'Winter',
    'february': 'Winter',
    'march': 'Spring',
    'april': 'Spring',
    'may': 'Spring',
    'june': 'Summer',
    'july': 'Summer',
    'august': 'Summer',
    'september': 'Autumn',
    'october': 'Autumn',
    'november': 'Autumn',
    'december': 'Winter'
}

现在你可以定义一个小函数来利用上面的字典。将根据检查用户的输入 seasons

如果找到,则可以在格式化字符串中返回。相反,它们可以作为 - 对返回。您也可以拨打lower()以获得更广泛的输入。

def user_prompt():
    """
    Here we ask for the month and use that
    month to do a 'lookup' in the seasons
    dictionary from above.

    :return: (str) formatted month and its season
    """
    month = input("Enter a month here: ")

    # call lower() to resolve capitalization differences
    if month.lower() in seasons.keys():
        return "{} is in the {}".format(month, seasons[month.lower()])
    else:
        return "{} is not a valid month.".format(month)


print(user_prompt())

这种模块化方法的另一个优点是能够在while循环中使用此类函数 - 如果您希望用户继续尝试键入正确的月份。

答案 2 :(得分:0)

您必须将input保存在任何变量中。

aa= input("enter month here. spelling and capitalization matter:")
if aa== "December" or aa== "January" or aa== "February":
    print(aa, "is in Winter")
elif aa== "March" or aa== "April" or aa== "May":
    print(aa, "is in Spring")
elif aa== "June" or aa== "July" or aa== "August":
    print(aa, "is in Summer")
elif aa== "September" or aa== "October" or aa== "November":
    Print(aa, "is in Autumn")
else:
    print("Check spelling")

答案 3 :(得分:0)

将输入存储在某个变量中。 您正在使用builtin_function_or_method来检查它是否等于。

month = input("enter month here. spelling and capitalization matter.")
if month == "December" or month == "January" or month == "February":
    print(month, "is in Winter")
elif month == "March" or month == "April" or month == "May":
    print(month, "is in Spring")
elif month == "June" or month == "July" or month == "August":
    print(month, "is in Summer")
elif month == "September" or month == "October" or month == "November":
    print(month, "is in Autumn")
else:
    print("Check spelling")