Python给了我"无"在尝试打印功能时

时间:2017-06-08 20:46:34

标签: python printing

我对python很新,现在只玩了几天。我正在拼凑一个程序,以展示我心中的战斗系统。我的问题是我不断得到"无"当我尝试打印时产生

import os
class Player():
    def __init__(self, name, hp, maxhp, strength, intel, charm, level):
        self.name = name
        self.hp = hp
        self.maxhp = maxhp
        self.strength = 10
        self.intel = intel
        self.charm = charm
    self.level = level
    def attack(self, Enemy):
        Enemy.hp -= self.strength

class Steve():
    def __init__(self, name, hp, maxhp, strength, intel, charm, level):
        self.name = name
        self.hp = hp
        self.maxhp = maxhp
        self.strength = strength
        self.intel = intel
    self.charm = charm
    self.level = level

class Barry():
    def __init__(self, name, hp, maxhp, strength, intel, charm, level):
        self.name = name
        self.hp = hp
        self.maxhp = maxhp
        self.strength = strength
        self.intel = intel
        self.charm = charm
        self.level = level
def Start():
    Player.hp = 100
    Player.maxhp = 200
    Player.strength = 30
    Player.intel = 10
    Player.charm = 43
    Player.level = 23
   nameSelection()


def nameSelection():
    os.system('cls')
    Player.name = input("Please enter your name\n==>")
    print("==>")
    gameStart()

def gameStart():
    os.system('cls')
    global Enemy
    print ("Player Name:",Player.name)
    print ("Who would you like to fight?")
    print ("1.)Steve")
    print ("2.)Barry")
    print ("3.)Change your name")
    option = input("==>")
    if option == "1":
        Enemy = Steve("Steve", 100, 200, 10, 20, 30, 50) 
        fight()      
    elif option == "2":        
        Enemy = Barry("Barry", 100, 200, 10, 20, 30, 50) 
        fight()  
    elif option == "3":
        nameSelection()

def attack():
    Enemy.hp -= Player.strength

def fight():
    os.system('cls')
    while Enemy.hp > 0:
        os.system('cls')
        print(CharacterStats())
        print ("1.) Attack")
        print ("2.) Run")
        option = input("==>")           
        if option == "1":
            attack()
            print (Enemy.hp)        
    print ("You did it! You have defeated", Enemy.name,"!")
    gameStart()

def CharacterStats():
    print ("You now face", Enemy.name)
    print ("His current health is", Enemy.hp,"/",Enemy.maxhp)
    print ("His strength is", Enemy.strength)
    print ("His intelligence is", Enemy.intel)
    print ("His charm is", Enemy.charm)
    print ("He is level", Enemy.level)
Start()

当我使用两个选项中的一个获得战斗限制器时,我得到了

You now face Steve
His current health is 100 / 200
His strength is 10
His intelligence is 20
His charm is 30
He is level 50
None
1.) Attack
2.) Run
==>

我已经读过,返回可以修复它,但我似乎无法回复给我想要的结果,不管它看起来有什么不同都打印到了播放器中,它只返回其中一个统计数据。我可能会把我的一些条款弄错,我希望我能清楚我的问题是什么。

3 个答案:

答案 0 :(得分:2)

你得到的是因为你是printing函数,你应该只调用函数,你的函数不返回任何东西,这就是你得到无的原因。 改变这一行:

print(CharacterStats())

为:

CharacterStats()

答案 1 :(得分:0)

在Python中,当你调用一个函数时,该函数会完成它的工作,然后返回一个值。例如,

def add(x, y):
   print("Working on it...")
   answer = x + y
   print("Got it!")
   return answer

调用时,此功能将执行四项操作。它将打印"在它上面工作......"到终点站。它将创建一个名为answer的变量,并将其分配给x和y的总和。它将打印"得到它"。然后,它将默默地返回该答案。

返回意味着什么?它意味着两件事:它告诉Python将控制权返回到调用函数的地方,它还说:无论我在哪里调用,都要用一定的值替换我。

所以,如果我写

print(add(2,3))

该功能将被调用并进行思考,打印"工作"和#34;得到它"到终点站。它将返回5,所以现在Python回到打印调用并用5替换add(2,3):

print(5)

将打印5.同样,我可以这样做:

 x = add(4,6) + add(5,2)

这将打印:      正在努力......      得到它了!      正在努力......      明白了!

然后x将设置为10 + 7或17。

在您的代码中,由于您不会从函数中显式返回任何内容,因此Python在到达函数的最后一行时使用默认的隐式返回None。这意味着

 print(MyFunc())

实际上被理解为,做功能,然后

 print(None)

希望能够解决问题!

答案 2 :(得分:0)

作为旁注,如果你在游戏中使用继承(使用Player类作为基类,例如:class Steve(Player):pass),它会更合适:https://docs.python.org/2/tutorial/classes.html