我试图迭代三个函数但得到此错误:NameError:未定义全局名称'dash'。我如何解决它 这是代码:
def draw():
name = input('Please type here: ').lower()#.split()
if name == 'what draw':
print('draw here')
else:
dash()
draw()
def paint():
name = input('Please type here: ').lower()#.split()
if name == 'what paint':
print('paint here')
else:
draw()
paint()
def erase():
name = input('Please type here: ').lower()#.split()
if name == 'what erase':
print('erase here')
else:
paint()
erase()
def dash():
name = input('Please type here: ').lower()#.split()
if name == 'what dash':
print('dash here')
else:
erase()
dash()
答案 0 :(得分:4)
你试图在它定义之前使用它。
def paint():
...
def draw():
...
def dash():
...
def erase():
...
draw()
paint()
erase()
dash()
答案 1 :(得分:2)
查看发生错误的行号。它写在例外中。 但我认为你的问题可能就在这里:
def draw():
name = input('Please type here: ').lower()#.split()
if name == 'what draw':
print('draw here')
else:
dash()
draw()
你调用draw()但是draw()将调用dash()而dash还没有定义
答案 2 :(得分:1)
按照目前的编写,您的代码:
dash()
;和这似乎是不明智的。您可以通过将输入与函数分开来解决这两个问题:
# function definitions here
while True:
name = input('Please type here: ').lower()
if 'dash' in name:
dash()
# repeat for other functions
elif 'quit' in name:
break
else:
print("Don't know that name.")