Python Forum
User input & Dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: User input & Dictionary (/thread-25363.html)



User input & Dictionary - tfernandes - Mar-28-2020

Hi All,

I'm just learning python.

I need user to enter Month and the print statement should print the complete Month
e.g.
Input = Jan
Output = January

#print(monthConversions[month]) works for me fine but I wanted to use a comparator to check if user has entered correctly and print Wrong Value if input is not appropriate.

monthConversions = {
    "Jan": "January",
    "Feb": "Februarzy",
    "Mar": "March",
    "April": "April",
    "May": "May",
    "June": "June",
    "July": "July",
    "Aug": "August",
    "Sep": "September",
    "Oct": "October",
    "Nov": "November",
    "Dec": "December",
}

month  = input("Enter month in three digit ")


if month == monthConversions:
    print(monthConversions[month])
else:
    print("Wrong Value ")

# print("You entered " + str(month))
After I run the code I get

Enter month in three digit Jan
Wrong Value





RE: User input & Dictionary - ndc85430 - Mar-28-2020

You're checking whether month is equal to the dictionary monthConversions, which will obviously never be True - values of different types are never equal. What comparison do you actually want? Do you want to check whether the input exists as a key in the dict, or something else?


RE: User input & Dictionary - buran - Mar-28-2020

You want to do
 
if month in monthConversions:
    print(monthConversions[month])
else:
    print("Wrong Value ")
however,there is more pythonic ways to do it
one would be
try:
    print(monthConversions[month])
except KeyError:
    print("Wrong Value")
the best would be

print(monthConversions.get(month, "Wrong Value"))



RE: User input & Dictionary - tfernandes - Mar-29-2020

(Mar-28-2020, 09:38 AM)ndc85430 Wrote: You're checking whether month is equal to the dictionary monthConversions, which will obviously never be True - values of different types are never equal. What comparison do you actually want? Do you want to check whether the input exists as a key in the dict, or something else?

hi thanks for the response.
I need the output to be January when I input Jan
if I input anything else it should be "Wrong Value"

In my case when I enter Jan im getting wrong value despite of Jan : January being available in the list.


RE: User input & Dictionary - ndc85430 - Mar-29-2020

Ok, so how do you get the value associated with "Jan" in the dict?


RE: User input & Dictionary - tfernandes - Apr-03-2020

(Mar-28-2020, 10:07 AM)buran Wrote: You want to do
 
if month in monthConversions:
    print(monthConversions[month])
else:
    print("Wrong Value ")
however,there is more pythonic ways to do it
one would be
try:
    print(monthConversions[month])
except KeyError:
    print("Wrong Value")
the best would be

print(monthConversions.get(month, "Wrong Value"))

Thanks alot the if month in month.conversations worked for me. This Is what I was exactly looking for.