-2

I need create more than one instances of a class based on user input. But after getting the userInput, I do not know what to do with it.

class Monster():
    def __init__(self):
        self.hp = 25
    def hit(self, hp):
        self.hp -= h

The code that gets the userInput for to create them, and the code that assigns each monster to a door.

def nMonsters():
    global nMonsters
    nMonsters = int(raw_input("How many monsters are hiding?"))
    if nMonsters >= nDoors:
        while nMonsters >= nDoors:
            clear()
            print ("Every monster needs a room.")
            print ("Rooms available"), nDoors - 1
            nMonsters = int(raw_input("How many monsters are hiding?"))
def monsterDoor_assignement():
    global monsterDoor
    monsterDoor = random.randint(1, nDoors) 
  • It seems you want not multiple classes, but multiple instances of one class? – glglgl Jun 18 '14 at 14:50
  • 1
    What exactly is the problem here? Do you know how to create one `Monster` instance? Do you know how to get the number of `Monster` instances to create? Where precisely are you stuck? – jonrsharpe Jun 18 '14 at 14:51
  • You should use a `list` to store your instances of `Monster`, and use a `for` loop to create x instances. – sloth Jun 18 '14 at 14:52
  • The code instantiating the classes looks like it relies on more logic that you describe in the post. A simple for loop would sufice for your description of the problem – omu_negru Jun 18 '14 at 14:52
  • I know how to create a single monster. lets so zombie=Monster(): But that only creates one. If the user wants to look for 5 monsters, how do I make that possible? I think I do mean instances. (only been coding for about 3 weeks now so the jargon is still new to me sorry) – Aka_Minimal Jun 18 '14 at 14:54

1 Answers1

0

To create x Monster instances, use a for loop or just a list comprehension:

monsters = [Monster() for _ in range(x)]

This gives you a list, where each item is a Monster instance.


Your use of global is a bad idea. Why not simply return the assigned door, and make the number of doors an argument?

def monster_door_assignment(doors): 
    return random.randint(1, doors)

Or, better, assuming you assign each Monster to a separate door:

def monster_door_assignment(doors, monsters):
    return random.sample(range(1, doors+1), monsters)

Using random.sample produces a list of unique random integers, one for each monster. You can now zip the returned list of integers with your list of Monster instances to assign each to their room:

list(zip(monsters, monster_door_assignment(doors, x)))
[(<__main__.Monster object at 0x0311FC30>, 2), 
 (<__main__.Monster object at 0x0311FCD0>, 4), 
 (<__main__.Monster object at 0x0311FCF0>, 9), 
 (<__main__.Monster object at 0x0311FD10>, 1), 
 (<__main__.Monster object at 0x0311FD30>, 7)]

Each item in the list is a two-tuple (monster, door number).


Your code for getting input is also awkward - see this SO community wiki for a guide on looping for valid input.

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • I will look more into the use of return. Thank you! – Aka_Minimal Jun 18 '14 at 15:03
  • @Aka_Minimal no problem. Relying on scope for variable access is bad - it makes development and testing much more difficult, and opens you up for all kinds of tricky bugs. Explicit arguments and return values means you can more easily test each function in isolation. – jonrsharpe Jun 18 '14 at 15:04
  • That's because you can't assign (`=`) and `return` at the same time. Try `return monster_deaths + 1`. – jonrsharpe Jun 18 '14 at 15:29
  • Going through and trying to remove all the global(s) but I'm just getting error after error. At this point I think I may just rewrite and work on learning returns as I go redo it. However; I have made some notes about what you've said. Thank you again for the advise! – Aka_Minimal Jun 18 '14 at 16:15
  • I think that is wise. Try to make lots of small functions, each of which does a single thing, taking everything it needs as an explicit argument and returning explicitly where necessary. If you've found this answer useful, you can indicate as much to other users by up-voting or accepting it - see http://stackoverflow.com/help/someone-answers – jonrsharpe Jun 18 '14 at 16:18
  • I'm also reading over that input guide, it's very insightful, with that and the return advise I'm fairly certain I can clean this project up a bit. – Aka_Minimal Jun 18 '14 at 16:59