Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Hello. I am doing a problem set 11 and I need to create a random initial position for the robot. The problem is, that whenever i try to use self.showLoc or self.showDir these functions generate a random integer each time, because they are assigned to randint(something). I would need the random integers generated only when the object is created and that when I call self.showLoc it wouldn't provide a new random integer. I would like to know how to assign my self.value to a randomly created value which is stored somewhere and doesn't change (all this should be done for each different object)

OpenStudy (anonymous):

and since it is done for each random object, this random variable creation method has to be inside the class describtion. I tried to use global variables, copy() etc., but it didn't work. Each time i call the methods on self I get a new random int. Thank you

OpenStudy (rsmith6559):

It sounds like showDir() and showLoc() are trying to do too many things. Why is something like setting a robots position, which then needs a random number being done by a display method?

OpenStudy (anonymous):

use a code pasting site to post your code if you would like. In the meantime: which class function runs when an instance is created? http://docs.python.org/2.7/tutorial/classes.html#a-first-look-at-classes

OpenStudy (anonymous):

Never mind, I fixed it. Or not fixed it, just understood how it works properly. Thanks anyway.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!