I want to create something like a "Switch case" object in Python, i'm using a dict and I want it to call a function when the key is used in my code.
This is the code
def main():
switch = {
1: incSearch(),
2: bisec()
}
switch.get(input(), lambda: print('Invalid option'))
The trouble cames that every time I run my script, it automatically executes the first method without letting the user input
You should use the function's name without calling it when storing in a dictionary:
switch = {
1: incSearch,
2: bisec
}
You should also call the function after getting it from the dict:
switch.get(input(), lambda: print('Invalid option'))()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.