Python Forum
Why does my Switch trigger all Cases?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my Switch trigger all Cases?
#1
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.
Reply
#2
There is no "switch" or anything else that calls a function in the code you posted.
Reply
#3
@woooee, case(input("\nPlease enter the win you wish to search for: ")) will call the case() function.
Reply
#4
IAMK - please post code directly rather than using links (which can cause security issues).
Read BBCODE on how to do this
Reply
#5
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:
Output:
λ python sw.py Please enter the win you wish to search for: 3OAK Hello1. λ python sw.py Please enter the win you wish to search for: 3aaa Not in dictionary
Reply
#6
(Apr-08-2018, 10:09 PM)snippsat Wrote: def case(s_input):
return {
'3OAK': f_3OAK(),
'4OAK': f_4OAK(),
'5OAK': f_5OAK()
}.get(s_input, 'Not in dictionary')

Thank you. This worked.

However, do you know why my code was printing all 3?
Reply
#7
(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: "))()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas, How to trigger parallel loop Mekala 4 556 Oct-29-2020, 12:58 PM
Last Post: Mekala
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 619 Sep-25-2020, 12:14 PM
Last Post: ndc85430
  Iterating through lists with different letter cases fatherted99 2 628 Jun-07-2020, 01:22 PM
Last Post: deanhystad
  How to trigger python script twice a day SriMekala 1 1,035 Aug-30-2019, 01:31 AM
Last Post: scidam
  Decorator staticmethod Use Cases Devarishi 3 845 May-20-2019, 04:27 AM
Last Post: Devarishi
  How to trigger a function by clicking the left mouse click? HelloBoyz 1 4,696 Jun-29-2017, 09:43 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020