0

I'm not sure why I keep getting an error for the "print func()" part, saying it's an invalid syntax. I'm trying to have the switcher go to a definition for each value it receives. The code runs the loop properly it's just the switcher part I'm having trouble with.

import numpy as np

i = 0

# get the order the balloons need to be popped in
# just put the number
yellow = input ("When does yellow get popped ")
red = input ("When does red get popped ")
blue = input ("When does blue get popped ")
green = input("When does green get popped ")

print("")

# put them in an array for the switcher function

order = [yellow, red, blue, green]


# a while loop that will go through the switcher until
# all ballons have been popped
while i < 4:

    def yellow_turn():
        return "pop yellow next"
 
    def red_turn():
        return "pop red next"
 
    def blue_turn():
        return "pop blue next"
 
    def green_turn():
        return "pop green next"

    next_ball = order[i]
    print(next_ball)

    def ballon_order(next_ball):
        switcher = {
        0: yellow_turn,
        1: red_turn,
        2: blue_turn,
        3: green_turn,
        }
        func = switcher.get(next_ball, lambda: "Invalid input")
        print func()
    i += 1
  • Are you using Python 2 or Python 3? From `print(next_ball)` I'm assuming you're using Python 3, and in that case you need brackets around `func()`, i.e. `print(func())`. Print is a function in Python 3 instead of a statement, so you need brackets if you want to use it. – Tyler Tian Apr 07 '21 at 05:13
  • I'm using Python 3 on VScode. I'll try that out. Thank you for the info. – MartzMM Apr 07 '21 at 21:19

1 Answers1

0

There are many reasons, this code is not working, for one if this is python 3 than print is a function and not a statement.

Also this is quiet wasteful in computation as each function is being redefined many times.

The function definitions can be put outside the while loop and it may be useful to make it a for loop instead.

so after all this the code looks something like this-

import numpy as np # This is not being used in the snippet but I will still include it
    
    
yellow = input ("When does yellow get popped ")
red = input ("When does red get popped ")
blue = input ("When does blue get popped ")
green = input("When does green get popped ")
    
#you also have to make these into integers to use them as keys later
yellow = int(yellow)
red = int(red)
blue = int(blue)
green = int(green)

def yellow_turn():
    return "pop yellow next"

def red_turn():
    return "pop red next"

def blue_turn():
    return "pop blue next"

def green_turn():
    return "pop green next"

switcher = {0:red_turn, 1:yellow_turn, 2:blue_turn, 3:green_turn}


#The loop will go like this
for i in (red,yellow,blue,green):
     print(switcher[i]())
   

However this code is very redundant and wasteful, it would be better to do it like this:

colors = ["red","yellow","blue","green"]

# list comprehensions

order = [int(input(f"when does {color} get popped : ")) for color in colors] # this creates a list of integers, in which the balloons will be popped the first will be red's number, second yellow's etc

for i in order:
    print(f"{colors[i]} will be popped next")

With and Output like this:

PS C:\Users\anubi\OneDrive\Documents> python st.py
when does red get popped : 3
when does yellow get popped : 1
when does blue get popped : 2
when does green get popped : 0
green will be popped next
yellow will be popped next
blue will be popped next
red will be popped next
  • Thanks for the feedback. The code I wrote is going to have a lot more to it and get more complicated. The "pop color next" part is just there until I input other parts of the code later but I also learned a few new things with your minimized code. Thank you. – MartzMM Apr 07 '21 at 21:23