【问题标题】:Python = in DEF [duplicate]Python =在DEF中[重复]
【发布时间】:2019-09-27 15:10:45
【问题描述】:

代码:

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

【问题讨论】:

  • 正确; notare 未定义。你的问题是为什么它没有被定义,还是你想要一些解决方法以便它bote中定义的?
  • 问题是你还没有在你的bote 函数中定义notare 是什么。您需要将它作为变量传递给bote。此外,这是一个非常基本的问题,您没有付出任何努力。仅仅粘贴你的代码和错误信息不会让人很乐意帮助你..

标签: python python-3.x list python-requests


【解决方案1】:

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

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

像这样

print("Starting...")

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

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


test()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2021-08-05
    • 2017-02-15
    相关资源
    最近更新 更多