Need help with python program
36 minutes ago, Celious said:I fixed it now, I needed to put
”global player_dmg” inside the functions
that's because you set it in the main scope and though you can reference it you need to set the global flag to change it.
This is why you should make a player class and update that value you wouldn't run into scope issues like this.
edit:
so in this situation you don't need to store player damage there is no point as it changes every time, what you should do is just return the damage form the function
import time
from random import randint
'''
creates our player class
'''
class Player:
#when first created
def __init__(self):
#set these
self.hp = 100
self.gold = 100
self.defence = 0
self.attack = 0
self.magic = 0
self.level = 1
#this is our light attack function it returns the attack value
def light_attack(self):
chance = randint(1, 100)
if chance < 20:
return 0
else:
return randint(10, 15)
#this is our heavy attack whcih also returns the attack value
def heavy_attack(self):
chance = randint(1, 100)
if chance < 40:
return 0
else:
return randint(20, 30)
'''
make player are Player class. you can create multi players like this
player2 = Player()
player3 = Player()
'''
player = Player()
#do are attacks 10 times
for i in range(10):
print('light attack %d' % player.light_attack())
print('heavy attack %d' % player.heavy_attack())
This will output something like this
light attack 14
heavy attack 0
light attack 11
heavy attack 0
light attack 15
heavy attack 0
light attack 13
heavy attack 0
light attack 15
heavy attack 0
light attack 15
heavy attack 29
light attack 11
heavy attack 23
light attack 11
heavy attack 30
light attack 0
heavy attack 29
light attack 13
heavy attack 22
You will probably want to set an attack value at the top and then roll between 1, self.attack. You could also move the rolling to it's own function so you don't have to keep typing the randomint each time.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now