-2

I'm trying to implement dict in python.

This code work perfectly:

  def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
    return switcher.get(argument, "nothing")

print (numbers_to_strings(0))

In this case the output is "zero".

But when I try to implement the one by input , I always get "nothing" :

def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
    return switcher.get(argument, "nothing")

print ("To write a number: ")
number = input ()
print (numbers_to_strings(number))

Best Regards.

NIN
  • 197
  • 13

2 Answers2

2

As pointed out by Daniel Roseman, input returns a string so simply change this line:

number = input ()

to: number = int(input())

gaw89
  • 1,018
  • 9
  • 19
0

The input is saving the number you enter as string. You need to parse number to integer before you call numbers_to_strings

DobromirM
  • 2,017
  • 2
  • 20
  • 29