Issue
Is there a way to reduce the amount of elif
things in my statement? In another question I asked someone said that my code suffered from redundancy and it definitely does. I'm relatively new to python and coding in general and when I look things up it's always kind of hard to understand.
This is the block of code I'm talking about:
(If needed I can post the rest of the code)
if (random_mon_name.lower()=='normal'):
rangen_mon_name = random.choice(mon_name_type_NORMAL)
break
elif (random_mon_name.lower()=='fire'):
rangen_mon_name = random.choice(mon_name_type_FIRE)
break
elif (random_mon_name.lower()=='water'):
rangen_mon_name = random.choice(mon_name_type_WATER)
break
elif (random_mon_name.lower()=='grass'):
rangen_mon_name = random.choice(mon_name_type_GRASS)
break
elif (random_mon_name.lower()=='electric'):
rangen_mon_name = random.choice(mon_name_type_ELECTRIC)
break
elif (random_mon_name.lower()=='ice'):
rangen_mon_name = random.choice(mon_name_type_ICE)
break
elif (random_mon_name.lower()=='fighting'):
rangen_mon_name = random.choice(mon_name_type_FIGHTING)
break
elif (random_mon_name.lower()=='poison'):
rangen_mon_name = random.choice(mon_name_type_POISON)
break
elif (random_mon_name.lower()=='ground'):
rangen_mon_name = random.choice(mon_name_type_GROUND)
break
elif (random_mon_name.lower()=='flying'):
rangen_mon_name = random.choice(mon_name_type_FLYING)
break
elif (random_mon_name.lower()=='psychic'):
rangen_mon_name = random.choice(mon_name_type_PSYCHIC)
break
elif (random_mon_name.lower()=='bug'):
rangen_mon_name = random.choice(mon_name_type_BUG)
break
elif (random_mon_name.lower()=='ghost'):
rangen_mon_name = random.choice(mon_name_type_GHOST)
break
elif (random_mon_name.lower()=='rock'):
rangen_mon_name = random.choice(mon_name_type_ROCK)
break
elif (random_mon_name.lower()=='dark'):
rangen_mon_name = random.choice(mon_name_type_DARK)
break
elif (random_mon_name.lower()=='dragon'):
rangen_mon_name = random.choice(mon_name_type_DRAGON)
break
elif (random_mon_name.lower()=='steel'):
rangen_mon_name = random.choice(mon_name_type_STEEL)
break
elif (random_mon_name.lower()=='fairy'):
rangen_mon_name = random.choice(mon_name_type_FAIRY)
break
else:
print("You didn't actually enter a type.\nTry Again.")
Solution
The root cause of there being so much redundancy in this code is the huge number of variables you have to manage.
Whenever you have a bunch of individual variables that all hold the same kind of data, like mon_name_type_FIRE
and mon_name_type_WATER
(I assume these are lists of names associated with that attack type), it's a strong clue that you should instead put them into a collection, like a dictionary, or a list. Not only does this reduce the number of variables to keep track of in your code, it makes it possible to access those values dynamically rather than with a ton of copy+pasted code.
For example, if you took all your different mon
s and put them in a dict
that's keyed by their attack type, instead of creating all the mon_name_type_WHATEVER
variables:
mon_by_type = {
"fire": ["charmander", "charmeleon", "charizard"],
"water": ["squirtle", "wartortle", "blastoise"],
# etc
}
you can now replace your giant if
/elif
chain with a single dictionary lookup:
try:
random_mon = random.choice(mon_by_type[random_mon_name.lower()])
except KeyError:
print("You didn't actually enter a type. Try again.")
Answered By - Samwise
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.