在python中调用一个函数,得到'(函数)没有定义'? [英] Call a function in python, getting '(function) is not defined'?

查看:881
本文介绍了在python中调用一个函数,得到'(函数)没有定义'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  ... 
workout = input( )

如果锻炼==y:
ballone()
elif锻炼==n:
print(好吧。)
sys.exit(如果你赢了,就不用了)
else:
sys.exit(Could not understand)

# #Ball one
def ballone():
...

问题正在呼叫'ballone'。你可以看到它在被命令行(ballone())调用时被定义和完美工作。

任何想法?我已经在网上搜索,但似乎找不到任何东西来帮助我。如果还有其他代码需要发布,请告诉我们:)

解决方案

将函数定义移动到之前

  def ballone():
#...

if workout ==y:
ballone()
elif workout ==n:
print(Okay。)
sys.exit(Not如果你赢了)
else:
sys.exit(Could not understand)

函数存储在标识符(变量)中,就像您的锻炼值一样。如果你没有先定义它,Python是如何知道它会在以后定义的?


I recently started learning Python and have some code here.

...
workout = input("Work out if you won?")

if workout == "y":
    ballone()
elif workout == "n":
    print("Okay.")
    sys.exit("Not working out if you won")
else:
    sys.exit("Could not understand")

##Ball one
def ballone():
...

The issue is calling 'ballone'. You can see that it is defined and works perfectly when called from the command line (ballone())

Any ideas? I have scoured the net but cannot seem to find anything to help me. If any more code needs posting then please let me know :)

解决方案

Move the function definition to before the lines that use it.

def ballone():
    # ...

if workout == "y":
    ballone()
elif workout == "n":
    print("Okay.")
    sys.exit("Not working out if you won")
else:
    sys.exit("Could not understand")

Functions are stored in identifiers (variables), just like your workout value. If you don't define it first, how is Python to know it'll be defined later?

这篇关于在python中调用一个函数,得到'(函数)没有定义'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆