anon
anon

Reputation: 13

python ignoring spelling even though it is right

My python code seems to be ignoring spelling and saying that it has wrong spelling.

I have tried moving around code changing the input types, this fixed a entry error but began this new error.

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")

Upvotes: 0

Views: 118

Answers (4)

eric
eric

Reputation: 1059

Here is a slightly different approach to your problem. There were a few syntax errors in your original post and this might help keep your code easier to read (easier to catch errors).

# 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'
}

Now you can define a small function that will take advantage of the dictionary above. The user's input will be checked against the keys in seasons.

If the key is found its value can be returned in a formatted string. Rather, they can be returned together as a key - value pair. You can also call lower() in order to allow for a wider range of inputs.

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())

The other advantage to this modular approach is the ability to use this kind of function in a while loop—in the event you wanted a user to keep trying to type a correct month.

Upvotes: 1

baduker
baduker

Reputation: 20042

You might want to do this like here:

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")

What you are missing is that you're not assigning a variable to the keyword input.

Upvotes: 2

Reck
Reck

Reputation: 1436

Store your input in some variable. You are using builtin_function_or_method to check if it equals.

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")

Upvotes: 0

Rohit-Pandey
Rohit-Pandey

Reputation: 2159

You have to save input in any variable.

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")

Upvotes: 0

Related Questions