首页 > 解决方案 > Python = 在 DEF 中

问题描述

代码:

print("Starting...")

def test():
    notare = input()
    bote()

def bote():
    if notare == "a":
        print("b")
    else:
        print("c")

test()

错误:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    test()
  File "test.py", line 5, in test
    bote()
  File "test.py", line 8, in bote
    if notare == "a":
NameError: name 'notare' is not defined

标签: pythonpython-3.xlistpython-requests

解决方案


python 不会知道该函数存在... Python 以有序的方式执行... 这意味着您必须先声明该函数,然后才能调用它...

所以对于你的问题..尝试将函数移到调用它的函数之上..

像这样

print("Starting...")

def bote(notare):
    if notare == "a":
        print("b")
    else:
        print("c")

def test():
    notare = input()
    bote(notare)


test()

推荐阅读