Re: back with more issues
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: back with more issues
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
Re: back with more issues
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
Re: back with more issues
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
Re: back with more issues
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
Re: back with more issues
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
Re: back with more issues
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 0x02954EA0 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
Re: back with more issues
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 0x02954EA0 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
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
Re: back with more issues
On Sun, Aug 11, 2013 at 11:33 PM, Kris Mesenbrink krismesenbr...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: back with more issues
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
Re: back with more issues
On Mon, Aug 12, 2013 at 12:35 AM, Kris Mesenbrink krismesenbr...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: back with more issues
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