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

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.

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

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!

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

Related

an enemy class in a RPG-game

I am in the midst of making an RPG game in Python.
I have made different classes, but I am having problems with one of my enemy types. It is a spider, and to spice things up, I tried to make the spider do dot (damage over time). It is not guaranteed poison damage, but when it does poison you, it stacks.
This is my parent class (enemy):
class Enemy():
def __init__(self, number_of, name):
self.number_of = int(number_of)
self.name = name
self.hp = 20 * self.number_of
self.dmg = 2 * self.number_of
def attack(self):
print(f"you now have {round(class1.hp)} health" + "\n")
def appearing(self):
if self.number_of > 1:
print (f"{self.number_of} {self.name}s have appeared")
else:
print(f"A {self.name} has appeared")
print (f"They have {self.hp} health")
Note: the class1 is the player
This is my spider class:
class Spider(Enemy):
posion_dmg = 0
def __init__(self, number_of, name):
Enemy.__init__(self, number_of, name)
self.posion_dmg = 0
def attack(self,player):
if self.posion_dmg > 0:
dot = 2 ** (self.poison_dmg + 1)
player.hp -= dot
print ("you are posioned" + "\n")
print (f"it deals {round(dot)} damage")
dmg = random.uniform(1.2 * self.dmg, 1.8* self.dmg)
player.hp -= dmg
print ("The spider(s) bite(s) you. ")
print(f"It deals {round(dmg)} damage")
if randint(1,4) == 1:
self.poison_dmg += 1
if self.number_of == 1:
print ("The spider poisons you")
else:
print ("The spiders poison you")
return Enemy.attack(self)
def appearing(self):
print ("*Spiders have a chance to hit you with poison that deals damage over time - this stack exponentially*")
return Enemy.appearing(self)
When I go in combat with the spider, and the randint is 1, it says that "Spider" has no attribute "poison_dmg", but I made it an attribute, right?
im kind of a newbie to python but i've successfully gone through the "Make Your Own Python Text Adventure" guide and i got a basic grip of how this works, although i do not completely understand how you're making the dot work, i think your problem is:
class Spider(Enemy):
posion_dmg = 0
def __init__(self, number_of, name):
Enemy.__init__(self, number_of, name)
self.posion_dmg = 0 <-----------------------HERE
in combination with this:
def attack(self,player):
if self.posion_dmg > 0:
dot = 2 ** (self.poison_dmg + 1)
player.hp -= dot
print ("you are posioned" + "\n")
print (f"it deals {round(dot)} damage")
dmg = random.uniform(1.2 * self.dmg, 1.8* self.dmg)
player.hp -= dmg
print ("The spider(s) bite(s) you. ")
print(f"It deals {round(dmg)} damage")
if randint(1,4) == 1: <----------------------------HERE
self.poison_dmg += 1
if self.number_of == 1:
print ("The spider poisons you")
else:
print ("The spiders poison you")
The if statement is indented within a def that begins with another if poison_dmg > 0, and is not because you've already defined it to be 0 in the init.
i would invert the order of the if statements within the attack function, so you first generate a poison_dmg value, and then you evaluate wether or not that value is greater than 0.
Hope it works!
PD1: This is my firs stackoverflow comment ever, sorry if i missed some answering standards.
PD2: English is not my mother tongue, please let me know if i didn't make myself clear at any point.

Problem with text adventure, global and local variables

from random import randint
from time import sleep
pminatk = 0
pmaxatk = 4
playerhp = 15
def atk(minatk, maxatk):
return randint(minatk, maxatk)
def playerAtk(monsterhp):
dmg = atk(pminatk, pmaxatk)
monsterhp -= dmg
print ("Du gjorde %i skade. Monsteret har nå %i liv igjen" % (dmg, monsterhp))
sleep(1)
return monsterhp
def monsterAtk(mminatk, mmaxatk):
global playerhp
dmg = atk(mminatk, mmaxatk)
playerhp -= dmg
print ("Monsteret gjorde %i skade. Du har nå %i liv igjen" % (dmg, playerhp))
sleep(1)
def fight(monsterhp, mminatk, mmaxatk):
global playerhp
while monsterhp > 0 and playerhp > 0:
playerAtk(monsterhp)
if monsterhp > 0:
monsterAtk(mminatk, mmaxatk)
if playerhp > 0:
print ("Gratulerer! Du beseiret monsteret!")
else:
print ("Du døde!")
fight(5, 1, 2)
fight(6, 0, 2)
This is supposed to be a simple battle system in a text based adventure. Now my problem is that monsterhp always goes back to its original valye after playerAtk is executed. If I choose to have monsterhp as a global variable it will remain 0 after fight() is finished, leaving the hp of all OTHER monster equal to zero as well. Now i could have more than one variable to assign different hp-values to different monster, but is there any way I can do this using parameters to the fight() function?
Your immediate problem is in your while loop when you call playerAtk(), you return monsterhp from it, but do not actually use it in that loop and that is why you see monster's health points to go back up to the original value. Line 28 needs to read:
monsterhp = playerAtk(monsterhp)
instead of:
playerAtk(monsterhp)
As it was suggested in a comment though, It's worth looking into defining your custom classes. I've thrown together following example based on your code to whet your appetite:
from random import randint
from time import sleep
PLAYERNAME = "Player1"
PLAYERHP = 15
PMINATK = 0
PMAXATK = 4
class CharacterDied(Exception):
def __init__(self, msg):
self.msg = msg
class Character:
def __init__(self, name, initial_hp, minatk, maxatk):
self.name = name
self.hp = initial_hp
self.minatk = minatk
self.maxatk = maxatk
def take_damage(self, damage_hp):
self.hp -= damage_hp
msg = "{} takes {:d}HP damage and has {:d} left."
print(msg.format(self.name, damage_hp, self.hp))
if self.hp < 0:
msg = "{} died."
raise CharacterDied(msg.format(self.name))
def attack(self, other):
dmg = randint(self.minatk, self.maxatk)
sleep(1)
other.take_damage(dmg)
def fight(char1, char2):
try:
while True:
char1.attack(char2)
char2.attack(char1)
except CharacterDied as death_exception:
print(death_exception.msg)
if __name__ == "__main__":
player = Character(PLAYERNAME, PLAYERHP, PMINATK, PMAXATK)
fight(player, Character("Monster1", 5, 1, 2))
if player.hp > 0:
fight(player, Character("Monster2", 6, 0, 2))
There are many ways to do this. For simplicity, I've written just one generic Character class we can use for player and monsters, but even if we started customizing them with subclasses, for instance the fight is now a bit more generic and we can also have a monster attack the player as it just became function of two fighting characters.
To your original question though the benefit sought after here is that attributes are kept and persistent with their instance objects which is generally much easier to follow than to try to deal with and track global variables.

Constantly checking a variable Python

Started trying to learn python yesterday, and I have run into a wall already T.T.
I am trying to make a health function in a game in python, and I need the variable health checked constantly to make sure it does not go below 0.
health = 10
health = (health - 5)
health = (health - 6)
Here I need the program to run a completely separate line of code, since health is now equal to -1. I do not want to have
if(health <= 0):
...
because I would need to copy paste this everywhere health is changed.
Would appreciate any help, thanks!
You don't need to check health constantly. Anytime you call a function reducing health (e.g. attack(character, damage)), you could simply check if health > 0. If not, you should call game_over().
Here's some code from a related question:
class Character:
def __init__(self, name, hp_max):
self.name = name
self.xp = 0
self.hp_max = hp_max
self.hp = hp_max
# TODO: define hp_bar here
def is_dead(self):
return self.hp <= 0
def attack(self, opponent, damage):
opponent.hp -= damage
self.xp += damage
def __str__(self):
return '%s (%d/%d)' % (self.name, self.hp, self.hp_max)
hero = Character('Mario', 1000)
enemy = Character('Goomba', 100)
print(enemy)
# Goomba (100/100)
hero.attack(enemy, 50)
print(enemy)
# Goomba (50/100)
hero.attack(enemy, 50)
print(enemy)
# Goomba (0/100)
print(enemy.is_dead())
# True
print(hero.xp)
# 100
I'm not familar with the python language, but my proposal can be tranferred to python.
Create a function that decreases the health value but never returns a value lower than zero. This is the pseudo-code:
function integer decreaseHealth(parameter health, parameter loss)
{
integer newHealth = health - loss
if (health < 0)
return 0
else
return newHealth
}
So I would need to type something like
if(health <= 0)"
print("Game over")
else:
print("New health")
Shame there isn't something in python for this, wish I could put before my code:
cont if(health <= 0):
print("Game over")
This would mean that whenever the health reached 0 or below, no matter where in the code after this, Game Over would print.
Then I wouldn't need to type anything when health is taken away apart from health = health - 1

How to call variable from one function in another file inside a function in another file?

I can't really copy over all of the code in both of these files since it is over 1000 lines, but essentially I am making a program that has the variables 'strength', 'endurance', 'strength', 'dexterity', 'intelligence', 'wisdom', and 'luck' (it's an rpg game) and I am trying to get the variable 'damage', which is an equation of 'strength' * 'dexterity' / 100, into another file. All of these variables are within the function character() in a file specifically for creating your character, and I'm trying to call over these variables again in another file for the main game, inside a variable called fight(). I've tried a multitude of things such as global variables, and using return, but nothing has worked for me. I'm sorry if I explained that poorly, comment if you have any questions.
The code in question.
character.py
def character():
#tons of stuff go here
global damage
damage = strength * dexterity / 100
game.py
def fight():
choice = input('(Type in the corresponding number to choose.) ')
global enemy_health
global damage
if choice == 1:
print ' '
print enemy_health,
enemy_health += -damage
print '-->', enemy_health
Thank you for your time.
I guess you could try importing character.py to game.py.
game.py: (edited)
import character
character_health = character.health
character_strength = character.strength
def fight():
...
But yeah, use classes.
Edit: example class
game.py:
class Character(object):
def __init__(self, health, strength):
self.health = health
self.strength = strength
self.alive = True
def check_dead(self):
self.alive = not(self.health)
def fight(self, enemy):
self.health -= enemy.strength
enemy.health -= self.strength
self.check_dead()
enemy.check_dead()
if __name__ == "__main__":
player = Character(300, 10) # health = 300, strength = 10
enemy = Character(50, 5) # health = 50, strength = 5
player.fight(enemy)
print("Player's health: {}\nIs he alive? {}\nEnemy's health: {}\nIs he alive? {}".format(player.health, player.alive, enemy.health, enemy.alive))

Passing on the parameters of a function into another one?

I'm not really sure how to word this, but I'll try my best. I've created a function for a zombie, and another one for attacks. But I don't know how to pass on the parameters of the zombie function into the attack function. Also if there is a more technical way to word this please let me know. Here is my test code for it, I'm sure this community will be able to figure out what I mean. The end goal is to create a function where I can plug in a monster name and have it's assigned parameters (health, defense, etc..) be run in through the combat function. ie def combat(monster_name): Any and all advice is much appreciated. Thank you!
#Functions
def zombie():
global zombie_hp
zombie_hp = 10
print "Zombie encounter"
print "Current Health:", zombie_hp
def attack(monster):
if monster > 0:
while monster > 0:
user_action = raw_input("Do you attack the zombie? [y]: ")
if user_action == "y":
monster_hp = monster_hp - 1
print "Health remaining: ", monster_hp
def user_action():
global user_action
user_action = raw_input("Do you attack the zombie? [y]: ")
# Zombie Encounter (attack_testing)
attack(zombie())
if zombie_hp == 0:
print "Zombie Defeated"
I suggest you use a class:
class Zombie:
def __init__(self):
self.defense = 10
self.hp = 100
def hit(self, hp):
self.hp-=hp
def upgrade(self, level):
self.defense+=level
This will remember the hp and defense for every zombie that you create
>>> Max = Zombie()
>>> Max.hit(17)
>>> Max.upgrade(2)
>>> Max.hp
83
>>> Max.defense
12
>>>

Categories