LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2011, 10:37 AM   #1
tavi
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Rep: Reputation: Disabled
Unhappy python - can I create objects with user input


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"

pet1 = PetClass("Kitty", 0.4, 0.6, 0.4)
pet2 = PetClass("Puppy", 0.5, 0.3, 0.6)
pet3 = PetClass("Fishy", 0.2, 0.2, 0.2)


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
 
Old 08-01-2011, 11:03 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by tavi View Post
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).
 
Old 08-01-2011, 01:36 PM   #3
tavi
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-01-2011, 02:16 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Actually it would look something like that:
Code:
# 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.
 
Old 08-02-2011, 01:47 PM   #5
Julian Andrews
LQ Newbie
 
Registered: Jan 2011
Distribution: Ubuntu
Posts: 21

Rep: Reputation: 13
Quote:
Originally Posted by TobiSGD View Post
Actually it would look something like that:
Code:
# Let user input the number of pets
number = input("How many?")
# Now create an empty array
yourpets = {}
Really, a list is more appropriate here:

Code:
number = input("How many?")
pets = []
for i in range(number):
    pets.append(PetClass(...))
or, more succinctly:

Code:
input("How many?")
pets = [PetClass(...) for i in range(number)]
A dictionary involves unnecessary overhead, and is really only appropriate if you need key lookup.
 
1 members found this post helpful.
Old 08-02-2011, 02:03 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by Julian Andrews View Post
Really, a list is more appropriate here:

Code:
number = input("How many?")
pets = []
for i in range(number):
    pets.append(PetClass(...))
or, more succinctly:

Code:
input("How many?")
pets = [PetClass(...) for i in range(number)]

A dictionary involves unnecessary overhead, and is really only appropriate if you need key lookup.
You are right, of course. Long time since I have done something with Python, I should do a fresh-up before giving advice.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python - using input to create new instance of a class comp_brad1136 Programming 5 04-08-2011 09:55 PM
LXer: PythonGTK Programming part 3: Screensaver, Objects, and User Input LXer Syndicated Linux News 0 06-11-2009 05:50 PM
python: list of class-objects? Valkyrie_of_valhalla Programming 3 08-24-2008 06:26 AM
Using user input to create target using Makefile makaveli_0000 Linux - General 0 07-02-2007 03:01 PM
ow to create script change a user's password? passwd expects input from keyboard.. Arodef Linux - General 1 03-02-2005 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration