answer3
是函数体中的一个名称,我想在另一个函数体中使用分配给它的对象。有没有办法做到这一点?
本质上,我想做的是:
def test():
answer3 = input("Direction?: ")
def thirdanswer():
if answer3 == "north":
do_something()
我不知道我在做什么,我真的很感激你的帮助。
正如其他人所建议的那样,通过基本的 Python 教程对您有好处。对于您的特定问题,您可以遵循以下框架:
def test():
return input("Direction?: ")
def thirdanswer():
if test() == "north":
print("success")
thirdanswer()
这运行thirdanswer()
,它调用test()
,它要求用户输入。然后,test()
将该输入返回到thirdanswer()
,然后将其与字符串“north”进行比较,如果匹配则打印成功。
另一种方法是使用全局变量。在下面的代码中,我有一个全局变量ans
。我明确调用test()
set ans
,然后调用与“north”thirdanswer()
进行比较ans
。
ans = ""
def test():
global ans
ans = input("Direction?: ")
def thirdanswer():
if ans == "north":
print("success")
test()
thirdanswer()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句