-1

I am new to python and have been working through a handful of activities. Currently I am working with Conditions. I created a dictionary of months and I am using an if/then condition to check if user input is in the dictionary. If the User input is not in the dictionary the output should say 'Bad Month' My code is as follows:

months = {1: 'January',
          2: 'February',
          3: 'March',
          4: 'April',
          5: 'May',
          6: 'June',
          7: 'July',
          8: 'August',
          9: 'September',
          10: 'October',
          11: 'November',
          12: 'December'}
choice = input

choice = input('Enter an integer value for a month:')
result = choice

if int(choice) in months:
    print('months')

else:
    print('Bad month')

When any integer above 12 is entered the output is 'Bad Month' but when I enter a number from 1-12 the output is just months? I've tried a number of print statements but none that I have tried work. I'm stuck.

E-Arsenal
  • 7
  • 1
  • 2
  • What do you want to print out? The month key? – Max Jul 17 '17 at 16:56
  • 1
    You probably want to do `print(months[int(choice)])`. – JohanL Jul 17 '17 at 16:58
  • 1
    Get rid of the condition and do: `print(months.get(int(choice), "Bad month"))` – Ben Jul 17 '17 at 17:02
  • possible duplicate of [of this question](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – sam-pyt Jul 17 '17 at 17:02
  • Possible duplicate of [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – sam-pyt Jul 17 '17 at 17:04
  • @9.0 He's already doing that check correctly. – Barmar Jul 17 '17 at 17:08
  • **the output is just months**. What else do you expect `print('months')` to do? What should it do instead? – Barmar Jul 17 '17 at 17:13

2 Answers2

2

You'll want to cast the user input from what input() takes as string to an integer that you can compare to the keys() of your dictionary, and print the corresponding value of that key.

months = {1: 'January',
          2: 'February',
          3: 'March',
          4: 'April',
          5: 'May',
          6: 'June',
          7: 'July',
          8: 'August',
          9: 'September',
          10: 'October',
          11: 'November',
          12: 'December'}

choice = int(input('Enter an integer value for a month: ')) # cast user input to integer

if choice in months:        # check if user input exists in the dictionary keys
    print(months[choice])   # print corresponding key value
else:
    print('Bad month')

Demo:

Enter an integer value for a month: 4
April
0

You can go several routes here. If you want to keep your code outline, try

if int(choice) in months:
    print('months')

else:
    print('Bad month')

As several comments suggested, a better approach might be to use the get syntax (tutorial).

months.get(input, "Bad Month") 

Will check for input and if it can't find it, return Bad Month. Just print what the get function returns, and it'll do what you are looking for.

patrick
  • 4,455
  • 6
  • 44
  • 61