I would like a user defined a series of strings
user_input = ('cat', 'cactus', 'cat')
which correspond to a series of objects to be instantiated from a dictionary of possible objects
classes = {
'cat': Cat,
'cactus': Cactus,
}
Where the Cat and Cactus correspond to classes which descend from a parent class
class Pet():
...
class Cat(Pet):
def __init__(self, name, colour):
Pet.__init__(self, name, colour)
...
class Cactus(Pet):
def __init__(self, name, colour):
Pet.__init__(self, name, colour)
...
I try to add object type to a list
pet_types = []
for i in range(0,3):
try:
pet_type.append(classes[user_input[i]])
except:
raise Exception('type no exist')
But when I do this the exception is raised telling me "type no exist" when the string corresponds exactly to the dictionary entry! Why is this happening?
I then want to use pet_types like this
pet_500 = pet_types[500](name,colour)
try...except. What is the actual exception being raised?range()when you could just loop everuser_inputdirectly.