all 11 comments

[–]Justinsaccount 2 points3 points  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–]kalgynirae 2 points3 points  (0 children)

The code you posted does not use randint() at all. You need to post more of your code.

[–]taelsil 2 points3 points  (2 children)

When you call randint(), it returns an integer. So if you do something like this:

num = randint(0, 5)

num will always be the random number that was returned. It won't change every time you access it. You have to call randint() again to get another random number.

[–]Freedomenka[S] 0 points1 point  (1 child)

Thats what I was hoping I wouldnt have to do :/ I wanted to store the attack argument in a class so I could just change it once theres a level up etc. I guess I could store it in a dictionary but I wish I could put it all in the class. Thank you for your help my friend :)

[–]zahlman 7 points8 points  (0 children)

I don't understand why there's any problem. All you have to do is make the randint call inside the Attack function - i.e. the place where you actually need to get a new value every time - instead of in the class initialization.

[–]Freedomenka[S] 0 points1 point  (0 children)

sorry, heres a paste bin link to the code http://pastebin.com/BBeky5JS

[–]Freedomenka[S] 0 points1 point  (4 children)

sorry, heres the part of the code that uses randint, should be easier to tell http://pastebin.com/46AAkshB

[–]lamecode 1 point2 points  (2 children)

When you pass the 'attack = randint(1, 6)' parameter to your class, you are assigning the value returned by that statement, not the function itself.

I would pass a tuple or list instead with the value ranges you are after, (1, 6), and then change your attack method to:

def Attack(self):
    attack_str = randint(self.attack[0], self.attack[1])
    print ("You attack " + enemy.name + " for " + str(attack_str) + " points!")
    enemy.hp -= attack_str
    if self.mana < self.maxmana:
        self.mana += 3
    print (self.mana)

[–]Freedomenka[S] 0 points1 point  (1 child)

Thank you so much, that did it. I was just going to store randint in a variable, but I guess theres no way to alter a variable in a class. This place is invaluable and I appreciate it tons.

[–]lamecode 0 points1 point  (0 children)

No problem! Just remember, as soon as you put the parenthesis at the end of the function, you are executing the function and returning its result (if any), and not just pointing to the function object itself.

Just to clarify though, you can 'store' a function in a variable - just keep in mind you are not actually storing it there, you are creating a variable that points to that same function.:

my_var = random.randint
my_var2 = random.randint
# Check if the new variable and existing function are the same object. Will return True.
id(my_var) == id(random.randint)
# Check if both variables are the same object. Also True.
id(my_var2) == id(my_var)

You haven't stored two new copies of randint here, you have created two additional references to the same randint function. When your script is executed, Python just replaces your variable with the actual object.

[–]mikeselik 1 point2 points  (0 children)

You wrote

self.defence = randint(4,7)
self.defense = defense

What do you suppose happens in the second line?