Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary trouble
#1
In the following code (which is executable as-is), I've created a dictionary called switcherx{} in the switcher() function. This function also calls other functions based on the selection from switcherx{}. This is the only place these functions are called from, and the only place I call switcher() is in the while loop at the bottom of the code. Nevertheless, even when I comment out the function call inside of switcher(), the functions still get called. Why is this? Furthermore, if I select "Stop" (1) from the code's menu, the tankSelect() function still gets called, even though the stop() function doesn't call it. I don't know if that's related to the first problem or not. Thanks for any tips.
def tankSelect():
 b = """
 1 = Tanks topped off
 2 = Tanks to tabs
 3 = Enter tank amounts
 """
 while(True):
  print(b)
  try:
   c = int(input("Selection: "))
   if ((c < 1) | (c > 3)):
    print("Invalid input1.")
    continue
   break
  except:
   print("Invalid input2.")
   continue
 if (c == 1):
  leftFuel = 40.5
  rightFuel = leftFuel
 elif (c == 2):
  leftFuel = 23.5
  rightFuel = leftFuel
 return c

def stop():
 return
	
def left():
 tankSelect()
 return
	
def right():
 tankSelect()
 return
	
def switch():
 tankSelect()
 return

def switcher(arg):
 switcherx = {
  1: stop(),
  2: left(),
  3: right(),
  4: switch()
 }
# func = switcherx.get(arg, "Invalid input7.")
# print(func)
 print("here3")
 return
	
a = """
1 to stop
2 to start on left tank
3 to start on right tank
4 to switch tanks
"""
while (True):
 print(a)
 try:
  y = int(input("Selection: "))
  switcher(y)
 except:
  print("Invalid input8.")
Reply
#2
When you use a dictionary for functions to be called, you assign just the function, not a call to the function.

def switcher(arg):
 switcherx = {
  1: stop,
  2: left,
  3: right,
  4: switch
 }
 if arg in switcherx:
  switcherx[arg]()
Note there are no parentheses in the definition of switcherx, but there parentheses on the last line when the function is called.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ah, yes. I had this problem a long time ago. The functions are being called when the switcherx is instantiated. There are two ways to refer to a function: by name and as a function call. The difference is the parentheses:

# Function by name, does not process
x = func

# Function call, does process
x = func()
Now, in the first example, x is instantiated as a copy, or alias, of func and the function can be called as:

x = func
fun() # Runs func()
x() # Also runs func()
So, in your code, the dictionary needs the function names without the parenthesis. Otherwise, they will all be called at instantiation of switcherx.

The second question is actually caused by the first problem. Because the dictionary called all the functions at instantiation, it's going to call tankSelect() regardless of your input.
Reply
#4
(Oct-08-2019, 12:51 AM)ichabod801 Wrote: switcherx[arg]()
Aaah, I never would have figured that out. Thanks so much. Solved my problem. Well, that one anyway. ;-)

(Oct-08-2019, 12:51 AM)stullis Wrote: So, in your code, the dictionary needs the function names without the parenthesis.
Thanks, stullis. ichabod801 said basically the same thing. Problem solved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 2,992 Sep-17-2019, 05:21 PM
Last Post: swechsler
  Trouble converting JSON String to Dictionary RBeck22 7 5,055 Mar-28-2019, 12:12 PM
Last Post: RBeck22
  Dictionary trouble test 6 3,286 Sep-19-2018, 10:27 PM
Last Post: test

Forum Jump:

User Panel Messages

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