ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I'm really new to Python. I'm writing a kind of virtual pet game at the moment in which 3 objects of the PetClass are created. But I was wondering if it is possible to make it so that the user decides how many objects of the class are created. So instead of something like
class PetClass(object):
def __init__(self, name, hunger, happiness, health):
self.name = name
self.hunger = hunger
self.happiness = happiness
self.health = health
print "Pet created"
I would want to have the user decide how many (up to 10) pets they will have/objects to be created. I thought maybe a for loop would do it:
number = input("How many?")
for i in range(number):
# create an object in PetClass
but I don't know what to write after that; I think it's probably impossible to create different objects this way, as they'd all have the same variable name (e.g. each one would be pet1) ...which would make them the same object. I have no idea how to give them all different parameters as well.
Is there maybe a way to give each object its own name and parameters maybe via a list and a count variable, or is it impossible/too fiendishly complex?
Last edited by tavi; 08-01-2011 at 10:41 AM.
Reason: my indentations aren't showing up
I think it's probably impossible to create different objects this way, as they'd all have the same variable name (e.g. each one would be pet1) ...which would make them the same object.
Look up arrays and use them. You might also want to either get user-defined values for each pet, or use random parameters (see the random module).
Do you know any good tutorial for using arrays with classes? They're not in my python book, and I can't really find anything so far that doesn't seem to assume prior knowledge of what arrays actually are.
I was hoping maybe there would be a way to turn a string element of a list into a variable name and use that to call an instance of the class...
e.g. mylist = ["pet1", "pet2", "pet3", "pet4"]
number = 3
for i in range(number):
count = 0
mylist[count] = PetClass()
count += 1
# Let user input the number of pets
number = input("How many?")
# Now create an empty array
yourpets = {}
# And fill it with the appropriate number of pets
for i in range(number):
yourpets[i] = PetClass( ... )
Of course you have to generate the values for the creation of the objects randomly (or in any other way you want).
Note for the future: When posting code (especially Python code, where the correct indentation is essential for the understanding of the programs) please use code-tags. You will get them when you click on the #-symbol on top of the post-editor. This will make your posts much more readable and helps significantly in helping you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.