我不确定为什么我不断收到“print func()”部分的错误,说它是无效的语法。我试图让切换器为它收到的每个值定义一个定义。代码正确运行循环,这只是我遇到问题的切换器部分。
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