Python 类函数未在主函数中定义

问题描述 投票:0回答:1
import random        
        
  
class Player:
    def __init__(self, name, weapon, strength, health, attack, hero, creature):
        self.name = name
        self.weapon = weapon
        self.strength = 100
        self.health = 100
        self.attack = 0
        self.hero = hero
        self.creature = creature
        
    def entername_player(self):
        self.name = input("Enter your name: ")
        print("Welcome " + self.name + " !")
        return self.name
      
    
    
    def main():
        hero = select_hero()
        
        
        
        
    if __name__ == "__main__":
        main()    

我试图剖析问题所在,并在添加其他功能之前测试我的一个功能,但我仍然对我的问题是什么感到困惑。

python string function error-handling undefined
1个回答
0
投票

从问题中尚不清楚最终结果需要是什么,但这将修复代码的当前结构:

  • 缩进
  • 需要创建该类的实例
  • 课堂上
  • 方法
    select_hero()
import random        
        
  
class Player:
    def __init__(self, name, weapon, strength, health, attack, hero, creature):
        self.name = name
        self.weapon = weapon
        self.strength = 100
        self.health = 100
        self.attack = 0
        self.hero = hero
        self.creature = creature
        
    def entername_player(self):
        self.name = input("Enter your name: ")
        print("Welcome " + self.name + " !")
        return self.name
      


    def select_hero(self):
        ...

    
def main():
    p = Player('darren', 'axe', 100, 100, 100, 'the moon', 'dragon')
    hero = p.select_hero()
    
    
    
    
if __name__ == "__main__":
    main()    

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