-1

For example, if user inputs a, program should print x, if user inputs b, program should print y, if user types c, program should print z. And so on.

Is there a way to make it without if elif elif elif elif elif...? I mean... It doesn't look right to me, there should be a better way to do so. Right?

synalice
  • 188
  • 8

1 Answers1

0

Dictionary will be the best for that

dc = {'a': 'x', 'b': 'y', 'c': 'z'}
a = input("input a character") #input c
print(dc.get(a, None))

Output

'z'

With the dictionary, you can map easily the desired output base on the user input.
In the get method of the dictionary, you can put a default value in case the dictionary doesn't have a map for a specific character.

Leo Arad
  • 4,452
  • 2
  • 6
  • 17