Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

back with more issues

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


krismesenbrink at gmail

Aug 11, 2013, 8:33 PM

Post #1 of 12 (24 views)
Permalink
back with more issues

import random

def player():
hp = 10
speed = 5
attack = random.randint(0,5)

def monster ():
hp = 10
speed = 4

def battle(player):
print ("a wild mosnter appered!")
print ("would you like to battle?")
answer = input()
if answer == ("yes"):
return player(attack)
else:
print("nope")


battle()


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's?
--
http://mail.python.org/mailman/listinfo/python-list


joel.goldstick at gmail

Aug 11, 2013, 9:21 PM

Post #2 of 12 (23 views)
Permalink
Re: back with more issues [In reply to]

On Sun, Aug 11, 2013 at 11:33 PM, Kris Mesenbrink
<krismesenbrink [at] gmail> wrote:
> import random
>
> def player():
> hp = 10
> speed = 5
> attack = random.randint(0,5)
# add the following line to return attack value:
return attack

>
> def monster ():
> hp = 10
> speed = 4
>
> def battle(player):
> print ("a wild mosnter appered!")
> print ("would you like to battle?")
> answer = input()
> if answer == ("yes"):
you don't need the parentheses around "yes"

> return player(attack)

you can't do that above because you defined the function with no
parameters. If you alter player to return attack you can alter the
above line to:
return player()

> else:
> print("nope")

Its a bad idea to have a function return something down one path (If
True), then return nothing down another path.
>
>
> battle()
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's?

I wrote some quick changes above to give you what you want. But you
need to understand more about functions. Your player function does
next to nothing. It defines two variables to fixed values, then gets
a random number and returns it. Can you try to explain what you think
each of your functions is doing? Every line should serve a purpose,
or it shouldn't be in the function.

> --
> http://mail.python.org/mailman/listinfo/python-list



--
Joel Goldstick
http://joelgoldstick.com
--
http://mail.python.org/mailman/listinfo/python-list


krismesenbrink at gmail

Aug 11, 2013, 9:35 PM

Post #3 of 12 (23 views)
Permalink
Re: back with more issues [In reply to]

the idea was to store variables for later use, but you are correct i don't understand functions or if that is even the best way to do it. i guess i'd want to be able to call the HP and ATTACK variables of player for when the battle gets called. i would then use the variables in battle to figure out who would win. is there a better way to store these variables in the functions? i also read somewhere about classes but that makes even less sense to me.
--
http://mail.python.org/mailman/listinfo/python-list


joel.goldstick at gmail

Aug 11, 2013, 9:51 PM

Post #4 of 12 (23 views)
Permalink
Re: back with more issues [In reply to]

On Mon, Aug 12, 2013 at 12:35 AM, Kris Mesenbrink
<krismesenbrink [at] gmail> wrote:
> the idea was to store variables for later use, but you are correct i don't understand functions or if that is even the best way to do it. i guess i'd want to be able to call the HP and ATTACK variables of player for when the battle gets called. i would then use the variables in battle to figure out who would win. is there a better way to store these variables in the functions? i also read somewhere about classes but that makes even less sense to me.
> --
> http://mail.python.org/mailman/listinfo/python-list

I'm not sure where you are learning. I really recommend you go to
python.org and go the the section on tutorials.
(http://wiki.python.org/moin/BeginnersGuide) They have some really
good stuff there to explain basics. Coding is fun, but until you
understand basic concepts its really more magic than anything else.
Also, you may get better help in the python-tutor group.

--
Joel Goldstick
http://joelgoldstick.com
--
http://mail.python.org/mailman/listinfo/python-list


davea at davea

Aug 11, 2013, 10:54 PM

Post #5 of 12 (17 views)
Permalink
Re: back with more issues [In reply to]

Kris Mesenbrink wrote:

> import random
>
> def player():
> hp = 10
> speed = 5
> attack = random.randint(0,5)
>
The net resut of this function is nothing. It assigns values, then
they're lost when the function returns. A function is the wrong way to
deal with these three names.

> def monster ():
> hp = 10
> speed = 4

Same here.

>
> def battle(player):

You probably want to have two parameters, player and monster

> print ("a wild mosnter appered!")
> print ("would you like to battle?")
> answer = input()
> if answer == ("yes"):
> return player(attack)
> else:
> print("nope")

This function makes no sense to me. A function should have three
well-defined pieces: what are its parameters, what does it do, what are
its side-effects, and what does it return. This function is confusing
on all of those.

>
>
> battle()
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's?

What you should want is a class for each type of character. At its
simplest, the class can be a storage place for related named
attributes.

You could make a class Player, which defines attributes called hp,
speed, and attack. Then later on you can refer to one of those
attributes with synatax like james.attack

class Player:
def __init__(self):
self.hp = 10
self.speed = 5
self.attack = random.randint(0,5)

Now, you create a player by
james = Player()

and the monster by
behemoth = Monster()

and you pass them into the function battle, by

result = battle(james, behemoth)

Inside the function, you'd say player.attack to see that random value.
And monster.speed to see behemoth's speed.


--
DaveA

--
http://mail.python.org/mailman/listinfo/python-list


krismesenbrink at gmail

Aug 11, 2013, 11:31 PM

Post #6 of 12 (17 views)
Permalink
Re: back with more issues [In reply to]

darn i was hoping i could put off learning classes for a bit, but it seems that is not the case. i have tested it a bit and it seems to be working correctly now.

++++++++++++++++++++++++++++
import random

class player():
hp = 10
speed = 5
attack = random.randint(0,5)

print (player.attack)

+++++++++++++++++++++++++++++++++++

i know it's not nearly as complicated as your examples but it seems to work. the self part of it always eluded me and continues to do so. and just so you know im learning through codecademy.com , it's based on python 2.7 and im trying to code in 3.3. but thanks for your help again and classes are starting (i think) to make some sort of sense.i'll have to reread both replies over and over again but it looks like a lot of useful info is there. but is the example i posted sorta right? i know i left the self part out but i think im on the right track.
--
http://mail.python.org/mailman/listinfo/python-list


krismesenbrink at gmail

Aug 11, 2013, 11:57 PM

Post #7 of 12 (16 views)
Permalink
Re: back with more issues [In reply to]

import random

class player():
hp = 10
attack = random.randint(0,5)

class monster():
hp = 10
attack = random.randint(0,4)


def battle():
print ("a wild mosnter appered!")
print ("would you like to battle?")
answer = input()
if answer == ("yes"):
while monster.hp >=0:
print ("you do", player.attack, "damage")
monster.hp -= player.attack
print (monster.hp)
elif answer == ("no"):
print ("you run away")
else:
print("you stand there")



battle()




Hello! just wanted to show you guys how its coming together, im starting to understand it abit more (hopefully it's right) at the moment it seems to only roll the attack once and uses that value but that's another issue all together that i bother you with (yet anyway).

thanks again guys you are awesome
--
http://mail.python.org/mailman/listinfo/python-list


davea at davea

Aug 12, 2013, 12:11 AM

Post #8 of 12 (17 views)
Permalink
Re: back with more issues [In reply to]

Kris Mesenbrink wrote:

> darn i was hoping i could put off learning classes for a bit, but it seems that is not the case. i have tested it a bit and it seems to be working correctly now.
>
> ++++++++++++++++++++++++++++
> import random
>
> class player():
> hp = 10
> speed = 5
> attack = random.randint(0,5)
>
> print (player.attack)
>
> +++++++++++++++++++++++++++++++++++
>
> i know it's not nearly as complicated as your examples but it seems to work. the self part of it always eluded me and continues to do so. and just so you know im learning through codecademy.com , it's based on python 2.7 and im trying to code in 3.3. but thanks for your help again and classes are starting (i think) to make some sort of sense.i'll have to reread both replies over and over again but it looks like a lot of useful info is there. but is the example i posted sorta right? i know i left the self part out but i think im on the right track.

The "complication" was there for good reason.

If you are sure you'll never have more than one player, this could work.
i don't see the advantage over (ugh) global variables, however.

But what happens when you have four monsters instead of one? A class
provides you a way to store data for each instance, not just for the
class as a whole. And the self convention is kind of analogous to the
English "myself." If you're inside an ordinary method, you refer to
yourself as "self."

By the way, by convention, class names are capitalized. That's why i
called it Player.
--
DaveA


--
http://mail.python.org/mailman/listinfo/python-list


sg552 at hotmail

Aug 12, 2013, 7:56 AM

Post #9 of 12 (10 views)
Permalink
Re: back with more issues [In reply to]

On 12/08/2013 06:54, Dave Angel wrote:
> [...]
>
> This function makes no sense to me. A function should have three
> well-defined pieces: what are its parameters, what does it do, what are
> its side-effects, and what does it return.

No! A function should have *four* well-defined pieces: what are its
parameters, what does it do, what are its side-effects, what does it
return, and an almost fanatical devotion to the Pope [etc.]
--
http://mail.python.org/mailman/listinfo/python-list


random832 at fastmail

Aug 12, 2013, 12:46 PM

Post #10 of 12 (6 views)
Permalink
Re: back with more issues [In reply to]

On Mon, Aug 12, 2013, at 10:56, Rotwang wrote:
> No! A function should have *four* well-defined pieces: what are its
> parameters, what does it do, what are its side-effects, what does it
> return, and an almost fanatical devotion to the Pope [etc.]

To be fair, I can't think of what "what does it do" includes other than
the side-effects and the return.
--
http://mail.python.org/mailman/listinfo/python-list


krismesenbrink at gmail

Aug 12, 2013, 8:13 PM

Post #11 of 12 (4 views)
Permalink
Re: back with more issues [In reply to]

the Classes and __init__ still don't make much sense actually. i have tried and tried again to make it generate numbers between 0 and 5 in a while statement but it just doesn't seem to be working.

import random


class Player():
hp = 10
def __init__(self, patt):
self.att = random.randint(0,5)



while Player.hp == 10:
print (Player.__init__)

atm it seems to be printing "<function Player.__init__ at 0x0000000002954EA0>" over and over again, i don't mind the repetition but from my understanding there should be numbers there. numbers that change. crazy frustrating that i just don't understand how this works.
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Aug 12, 2013, 8:31 PM

Post #12 of 12 (3 views)
Permalink
Re: back with more issues [In reply to]

On 13/08/2013 04:13, Kris Mesenbrink wrote:
> the Classes and __init__ still don't make much sense actually. i have tried and tried again to make it generate numbers between 0 and 5 in a while statement but it just doesn't seem to be working.
>
> import random
>
>
> class Player():

This sets an attribute of the class:

> hp = 10

This method will be called to initialise an instance of the class when
one is created:

> def __init__(self, patt):

This sets an attribute of the instance:

> self.att = random.randint(0,5)
>
>
>
> while Player.hp == 10:

This prints the __init__ method of the class:

> print (Player.__init__)
>
> atm it seems to be printing "<function Player.__init__ at 0x0000000002954EA0>" over and over again, i don't mind the repetition but from my understanding there should be numbers there. numbers that change. crazy frustrating that i just don't understand how this works.
>
At no point does it create an instance of the class, so the __init__
method is never called.

You can't return anything from the __init__ method because it's called
just to initialise the instance.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.