Skreety
Skreety

Reputation: 33

How do I use a variable to call a class?

Code:

class Mega:
    def __init__(self, name, types, moves):
        self.name = name
        self.types = types
        self.moves = moves
  
ropher=Mega('Ropher', 'Sound', ['Screech', 'Coil'])
mijek=Mega('Mijek', 'Light', ['Solar Beam', 'Healing Sunlight'])

input=input('What mega? ').lower
print(f"your mega is {input.name}, a {input.types} type")

I want to use the user input to decide which Mega to call, but I can't find a way to go about it. The idea is to get it to print Ropher's name and type if the input is 'ropher', and Mijek's name if the input is 'mijek'. Can someone help?

Upvotes: 1

Views: 391

Answers (3)

alani
alani

Reputation: 13079

It is better to use a dictionary instead of separate variables. You then use a dictionary lookup to find the ones that you want.

class Mega:
    def __init__(self, name, types, moves):
        self.name = name
        self.types = types
        self.moves = moves

megas = {'ropher': Mega('Ropher', 'Sound', ['Screech', 'Coil']),
         'mijik': Mega('Mijek', 'Light', ['Solar Beam', 'Healing Sunlight'])}

key = input('What mega? ').lower()
print(f"your mega is {megas[key].name}, a {megas[key].types} type")

You should also not call your variable input because you override the builtin function. In the above, I have renamed it as key.

Note that if the key that you try to look up does not exist in the dictionary, then your program will stop with a KeyError. If you want to prevent this, you can first use in to check whether the input exists as one of the keys in the dictionary:

if key in megas:
    print(f"your mega is {megas[key].name}, a {megas[key].types} type")
else:
    print("not a valid mega")

or you can catch the KeyError:

try:
    print(f"your mega is {megas[key].name}, a {megas[key].types} type")
except KeyError:
    print("not a valid mega")

Upvotes: 6

tdelaney
tdelaney

Reputation: 77347

You have multiple objects that you want to find by a common feature. In this case the lower-cased name. You can use a dict to index your objects by name and then find them easily later.

class Mega:
    def __init__(self, name, types, moves):
        self.name = name
        self.types = types
        self.moves = moves
  
mega_index = {}
ropher=Mega('Ropher', 'Sound', ['Screech', 'Coil'])
mega_index[ropher.name.lower()] = ropher
mijek=Mega('Mijek', 'Light', ['Solar Beam', 'Healing Sunlight'])
mega_index[mijek.name.lower()] = mijek

my_input=input('What mega? ').lower()
try:
    my_mega = mega_index[my_input]
    print(f"your mega is {my_mega.name}, a {my_mega.types} type")
except KeyError:
    print("No such mega")

This is just the tip of the iceberg. You really didn't need those ropher and mijek variables at all. They are in the dictionary and now can be acted on dynamically.

Upvotes: 3

user
user

Reputation: 675

What you want is a dictionary:

megas = {
    'ropher': Mega('Ropher', 'Sound', ['Screech', 'Coil']),
    'mijek': Mega('Mijek', 'Light', ['Solar Beam', 'Healing Sunlight']),
}

input=megas[input('What mega? ').lower()]
print(f"your mega is {input.name}, a {input.types} type")

Upvotes: 1

Related Questions