Why does my Switch trigger all Cases? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why does my Switch trigger all Cases? (/thread-9432.html) |
Why does my Switch trigger all Cases? - IAMK - Apr-08-2018 Hello, I'm a first time user of Python, but I have c++/VB.Net experience. I absolutely dread Python's syntax, but I need to learn it. What is wrong with my implementation of this switch? Instead of only calling the function of the matching string, it calls all 3. def f_3OAK(): print("Hello1.") return False def f_4OAK(): print("Hello2.") return False def f_5OAK(): print("Hello3.") return False def case(s_Input): #My definition of a Case Switch. return { '3OAK': f_3OAK(), '4OAK': f_4OAK(), '5OAK': f_5OAK() }[s_Input] while True: case(input("\nPlease enter the win you wish to search for: "))Thank you in advance. RE: Why does my Switch trigger all Cases? - woooee - Apr-08-2018 There is no "switch" or anything else that calls a function in the code you posted. RE: Why does my Switch trigger all Cases? - IAMK - Apr-08-2018 @woooee, case(input("\nPlease enter the win you wish to search for: ")) will call the case() function.
RE: Why does my Switch trigger all Cases? - Larz60+ - Apr-08-2018 IAMK - please post code directly rather than using links (which can cause security issues). Read BBCODE on how to do this RE: Why does my Switch trigger all Cases? - snippsat - Apr-08-2018 Some changes, get() is nice as it also can span a message(None default) it something is not found.return in all function.def f_3OAK(): return("Hello1.") def f_4OAK(): return("Hello2.") def f_5OAK(): return("Hello3.") def case(s_input): return { '3OAK': f_3OAK(), '4OAK': f_4OAK(), '5OAK': f_5OAK() }.get(s_input, 'Not in dictionary') user_input = input("\nPlease enter the win you wish to search for: ") print(case(user_input))Test:
RE: Why does my Switch trigger all Cases? - IAMK - Apr-09-2018 (Apr-08-2018, 10:09 PM)snippsat Wrote: def case(s_input): Thank you. This worked. However, do you know why my code was printing all 3? RE: Why does my Switch trigger all Cases? - snippsat - Apr-09-2018 (Apr-09-2018, 09:21 AM)IAMK Wrote: However, do you know why my code was printing all 3?Function calls in return dictionary will trigger all call. Could do it like this. def f_3OAK(): print("Hello1.") def f_4OAK(): print("Hello2.") def f_5OAK(): print("Hello3.") def case(s_Input): return { '3OAK': f_3OAK, '4OAK': f_4OAK, '5OAK': f_5OAK }[s_Input] while True: case(input("\nPlease enter the win you wish to search for: "))() |