“返回外部函数”

2024-09-30 06:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

经过一些研究,我无法找出为什么我在这个codeaccademy练习中得到指定的错误(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

Tags: andtonameselfyouformatinputyour
1条回答
网友
1楼 · 发布于 2024-09-30 06:27:23

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())

相关问题 更多 >

    热门问题