Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52398 > unrolled thread

back with more issues

Started byKris Mesenbrink <krismesenbrink@gmail.com>
First post2013-08-11 20:33 -0700
Last post2013-08-13 06:22 +0000
Articles 14 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 20:33 -0700
    Re: back with more issues Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-12 00:21 -0400
      Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 21:35 -0700
        Re: back with more issues Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-12 00:51 -0400
    Re: back with more issues Dave Angel <davea@davea.name> - 2013-08-12 05:54 +0000
      Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 23:31 -0700
        Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 23:57 -0700
        Re: back with more issues Dave Angel <davea@davea.name> - 2013-08-12 07:11 +0000
      Re: back with more issues Rotwang <sg552@hotmail.co.uk> - 2013-08-12 15:56 +0100
        Re: back with more issues random832@fastmail.us - 2013-08-12 15:46 -0400
          Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-12 20:13 -0700
            Re: back with more issues MRAB <python@mrabarnett.plus.com> - 2013-08-13 04:31 +0100
            Re: back with more issues Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-12 23:48 -0400
            Re: back with more issues Steven D'Aprano <steve@pearwood.info> - 2013-08-13 06:22 +0000

#52398 — back with more issues

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-11 20:33 -0700
Subjectback with more issues
Message-ID<cc701481-0ecd-411b-8473-0596b4252949@googlegroups.com>
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?

[toc] | [next] | [standalone]


#52399

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-08-12 00:21 -0400
Message-ID<mailman.490.1376281271.1251.python-list@python.org>
In reply to#52398
On Sun, Aug 11, 2013 at 11:33 PM, Kris Mesenbrink
<krismesenbrink@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#52402

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-11 21:35 -0700
Message-ID<aa30df64-2545-4352-8819-264ba696b48f@googlegroups.com>
In reply to#52399
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.

[toc] | [prev] | [next] | [standalone]


#52404

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-08-12 00:51 -0400
Message-ID<mailman.492.1376283580.1251.python-list@python.org>
In reply to#52402
On Mon, Aug 12, 2013 at 12:35 AM, Kris Mesenbrink
<krismesenbrink@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#52406

FromDave Angel <davea@davea.name>
Date2013-08-12 05:54 +0000
Message-ID<mailman.493.1376286900.1251.python-list@python.org>
In reply to#52398
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

[toc] | [prev] | [next] | [standalone]


#52407

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-11 23:31 -0700
Message-ID<441b03e6-4280-4abf-9987-bb4aea81ae6d@googlegroups.com>
In reply to#52406
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.

[toc] | [prev] | [next] | [standalone]


#52408

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-11 23:57 -0700
Message-ID<180b67b6-b69a-43d5-a7f4-f5d63ce66479@googlegroups.com>
In reply to#52407
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

[toc] | [prev] | [next] | [standalone]


#52410

FromDave Angel <davea@davea.name>
Date2013-08-12 07:11 +0000
Message-ID<mailman.494.1376291526.1251.python-list@python.org>
In reply to#52407
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

[toc] | [prev] | [next] | [standalone]


#52418

FromRotwang <sg552@hotmail.co.uk>
Date2013-08-12 15:56 +0100
Message-ID<kuat2n$msi$1@dont-email.me>
In reply to#52406
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.]

[toc] | [prev] | [next] | [standalone]


#52427

Fromrandom832@fastmail.us
Date2013-08-12 15:46 -0400
Message-ID<mailman.509.1376337204.1251.python-list@python.org>
In reply to#52418
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.

[toc] | [prev] | [next] | [standalone]


#52434

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-12 20:13 -0700
Message-ID<88f95aab-0274-48ee-a68a-14a8c3b36e9c@googlegroups.com>
In reply to#52427
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.

[toc] | [prev] | [next] | [standalone]


#52436

FromMRAB <python@mrabarnett.plus.com>
Date2013-08-13 04:31 +0100
Message-ID<mailman.518.1376364680.1251.python-list@python.org>
In reply to#52434
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.

[toc] | [prev] | [next] | [standalone]


#52438

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-08-12 23:48 -0400
Message-ID<mailman.520.1376365707.1251.python-list@python.org>
In reply to#52434
On Mon, 12 Aug 2013 20:13:31 -0700 (PDT), Kris Mesenbrink
<krismesenbrink@gmail.com> declaimed the following:

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

1)	You fail to create an INSTANCE of a Player...

2)	You set a classwide (ie, ALL instances will have the value) "hp" to a
value of 10

3)	You then loop AS LONG AS the classwide value is 10 (ie; forever)

4)	 You are printing a reference to the __init__ method of the class

5)	 You defined __init__ to take some argument, which you need to pass to
the instance creation


	__init__ is called when you create an instance of the class... that is:

instance = Player()

	Unless you do something IN the __init__ method to change self.hp the
loop will never end.


class Player(object):	#I'm still in Python 2.x, so inheriting from object
						#is needed to get a "new-style" class

	def __init__(self, patt):
		self.hp =  random.randint(0,11)
		self.patt = patt		#no idea what you  intend to do with this

while True:
	aplayer = Player("Junk")	#"Junk" will be bound to self.patt
								#aplayer is an instance of Player with
								#a random hp value
	print aplayer.hp
	if aplayer.hp == 10: break

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#52439

FromSteven D'Aprano <steve@pearwood.info>
Date2013-08-13 06:22 +0000
Message-ID<5209d0bd$0$29885$c3e8da3$5496439d@news.astraweb.com>
In reply to#52434
On Mon, 12 Aug 2013 20:13:31 -0700, 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.

Hi Kris,

You might also find that the tutor mailing list is a better match for
your status as a complete beginner to Python. You can subscribe to it
here:

http://mail.python.org/mailman/listinfo/tutor


If you do, please take my advice and use individual emails, not daily
digests. It is MUCH easier to carry on a back-and-forth conversation with
individual emails.


But for now, you seem to have misunderstood what your code is doing. 
Let's start with the basics:

The method __init__ is automatically called by Python when you create a 
new instance. You almost never need to call it by hand, so 99% of the 
time, if you're writing something like "Player.__init__",  you're 
probably making a mistake.

But when you do want to call a method -- and remember, you're not 
manually calling __init__ -- you need to put round brackets (parentheses 
for Americans) after the method name. So you would say something like:

Player.__init__(arguments go inside here)

rather than just Player.__init__. Without the parentheses, you're just 
referring to the method, not actually calling it. And without the right 
number and type of arguments, you'll get an error.


So how do you create a new Player? Easy -- you just call the *class*, as 
if it were a function:

fred = Player()
barney = Player()
wilma = Player()
betty = Player()

Take note of the round brackets. If you leave them out, each of fred, 
barney, wilma, betty will be aliases to the Player class, rather than 
separate players.

So that's the first thing. Now, another potential problem. Your class 
starts off like this:

class Player():
    hp = 10
    ...more code follows


What this does is set a class-wide attribute called "hp", which every 
instance shares. Does this matter? Maybe not, it depends on how you use 
it. Your sample code doesn't show enough to tell if it will be a problem 
or not.

Next, you write this:


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

but since nothing in the loop changes the value of Player.hp, this will 
loop forever, or until you interrupt it. So I'm not really sure what you 
actually intend to do.

My guess is that what you actually want is something like this:


for i in range(10):
    player = Player()
    print("Player", i, "has value", player.attr)


This ought to get you started.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web