Wagromph
Wagromph

Reputation: 13

Python gives me "None" when trying to print a function

I am quite new to python, having only toyed with it for a few days now. I am trying to cobble together a program to show off a battle system I had in mind. My issue is that I keep getting "None" produced when I try to print

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

When I get to a fight encoutner with one of the two options, I get

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
==>

I have read that return could fix it, but I can't seem to get return to give me the desired results I was looking for, both how it looks different went printed in to the player, and it only returns one of these lines of stats. I may be getting some of my terms wrong, I hope I am being clear on what my issue is.

Upvotes: 0

Views: 257

Answers (3)

dejanualex
dejanualex

Reputation: 4348

As a side note, it will be more suitable if you use inheritance (use the Player class as base class eg: class Steve (Player):pass ) in your game : https://docs.python.org/2/tutorial/classes.html

Upvotes: 0

Alex Lew
Alex Lew

Reputation: 2124

In Python, when you call a function, that function does its work and then returns a value. For instance,

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

When called, this function will do four things. It will print "Working on it..." to the terminal. It will create a variable called answer, and assign it to the sum of x and y. It will print "Got it". And then, it will silently return that answer.

What does return mean? It means two things: it tells Python to return control to the place where the function was called, and it also says: wherever I was called, replace me with a certain value.

So if I write

print(add(2,3))

the function will be called and do its thinking, printing "working" and "got it" to the terminal. It will return 5, so now Python comes back to the print call and replaces add(2, 3) with 5:

print(5)

which will print 5. Similarly, I could do:

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

This would print: Working on it... Got it! Working on it... Got it!

And then x would be set to 10 + 7, or 17.

In your code, since you don't explicitly return anything from your function, Python uses the default, implicit return of None when it reaches the last line of your function. That means that

 print(MyFunc())

actually is understood as, do the function, then

 print(None)

Hope that clears things up!

Upvotes: 1

Mohd
Mohd

Reputation: 5613

You are getting None because you are printing the function, where you should just call the function, your function doesn't return anything and that is why you are getting None. Change this line:

print(CharacterStats())

to:

CharacterStats()

Upvotes: 3

Related Questions