为什么返回一个值,然后将它用作变量不能以这种方式在Python中工作? [关闭]

问题描述 投票:-3回答:3
import time
def planeFun ():
    print("The plane has spiked your intrest")
    print("  s    ")
    skully=input("You ran a couple feet away, are you going to go look back in?\n")
    if skully in ["Yes", "yes", " yes", " Yes", "y", "Y", "ya"]:
        print("You decide to go and check back in, make sure what you saw is real!")
        goin=input("should you investigate the plane more, or run away?\n")
        return goin


planeFun()

result= planeFun()
if result in ["run", "Run", " run", " Run", "run away", "Run away", " run away", " Run away"]:
    print("You decide to leave, something weird is going on, and you're not sticking around to find out")

我在这个程序中做错了什么?我试图解决它,但不能。每当输入'run'时,程序重新运行该功能,我无法弄清楚原因。

python function variables return return-value
3个回答
0
投票

每当输入'run'时,程序重新运行该功能,我无法弄清楚原因。

这是由于planeFun函数被调用两次:

planeFun()          # <-- 1

result= planeFun()  # <-- 2

除此之外,我还会推荐@ Jean-FrançoisFabre在评论中提到的内容。删除尾随和前导空格并使输入不区分大小写可以更容易检查某些结果:

def planeFun ():
    print("The plane has spiked your intrest")
    print("  s    ")
    skully=input("You ran a couple feet away, are you going to go look back in?\n").lower().strip()

这样你只需要检查小写文本,你可以删除大写重复检查。


3
投票

倒数第二行,您键入:

`if reslut in ...`

尝试结果。


0
投票

您应该填写“else”语句进行调试,意味着如果输入结果与输入不一致则打印

© www.soinside.com 2019 - 2024. All rights reserved.