What is the alternative of if-elif-else statements in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the alternative of if-elif-else statements in this code?

Explain https://code.sololearn.com/cpXpVG2hSJtL/?ref=app

24th May 2020, 8:25 AM
ROHIT PRADHAN
ROHIT PRADHAN - avatar
2 Answers
+ 3
An alternative could be defining all options as methods (using def for each of them). Then, making a dictionary with possible input as keys and method calls as values: def check_balance(): ... d = {'1': check_balance, ...} u = input() if u in d: d[u]() else: print('Wrong choice') EDITED, thanks HonFu!
24th May 2020, 8:32 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
Kuba Siekierzyński, wouldn't it have to go like this? d = {'1': check_balance, ...} u = input() if u in d: d[u]() else: print('Wrong choice')
24th May 2020, 8:41 AM
HonFu
HonFu - avatar