Jump to content

Need help with python program

Go to solution Solved by vorticalbox,
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.

import time
import random
from random import randint

#Player stats
player_hp = 100
player_gold = 100
#Player skills
Defence = 0
Attack = 0
Magic = 0
Level = 1
#Enemy stats
enemy_hp = 100
enemy_dmg = 5

player_dmg = ''


def light_attack():
    chance = (randint(1,100))
    if chance <20:
        player_dmg = 0
        return player_dmg
    else:
        player_dmg = (randint(10,15))
        return player_dmg
 


def heavy_attack():
    chance = (randint(1,100))
    if chance <40:
        player_dmg = 0
        return player_dmg
    else:
        player_dmg = (randint(20,30))
        return player_dmg
   

heavy_attack()  
print (player_dmg)




for some reason it doesn't update the value of  "player_dmg" so it prints nothing

Link to comment
https://linustechtips.com/topic/851859-need-help-with-python-program/
Share on other sites

Link to post
Share on other sites

You've run the heavyattack method but haven't used the returned value anywhere. I'm not sure how it works in python but try putting 

player_dmg = heavy_attack()  

instead of the line

heavy_attack()

at the bottom

I edit my posts a lot.

Link to post
Share on other sites

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.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×