繁体   English   中英

“返回外部功能”错误

[英]“return outside function” error

经过一些研究,我无法找出为什么在此代码学院练习中遇到指定的错误( SyntaxError: 'return' outside function return'outside SyntaxError: 'return' outside function )。

name=input(" input name ")
print("welcome, {}".format (name))

class player():

    def __init__(self, atk, hp,):
        self.atk=atk
        self.hp=hp

    def __str__(self):
        return "{}, {}".format (self.atk, self.hp)

input("Time for a questionnaire to define your stats")
input("press enter to continue....")
print("in an intense battle when both you and your enemy are on the edge of consciousness and you have a chance to flee do you finish off the opponent taking a chance on being struck down or do you flee with your life?")
statq1=input("fight or flee")
if statq1 == "fight":
    return 5+self
elif statq1 == "flee":
    return 5+hp

return必须在函数内部。 现在,该代码位于脚本的基础上,因此您可以将5+self保存为变量以供以后使用,也可以将其打印到屏幕上。

使其成为一个函数:

def start():
    input("Time for a questionnaire to define your stats")
    input("press enter to continue....")
    print("in an intense battle when both you and your enemy are on the edge of consciousness and you have a chance to flee do you finish off the opponent taking a chance on being struck down or do you flee with your life?")
    statq1=input("fight or flee")
    if statq1 == "fight":
        return 5+self
    elif statq1 == "flee":
        return 5+hp

print(start())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM