This is my first time asking a question here, so if I leave something out please forgive me.
I am a beginner programmer and I'm starting with python 3.4.
I have a function(below) that I have made and it works fine when executed, but I am trying to compress my code footprint.
def PwrPriSel (PwrPri):
#Physical Abilities
Dismiss1 = "I'm sorry, but you are not the person I have been waiting for."
Dismiss2 = "Please answer the question"
PhyPwr = 'Strength, Flight, Speed, Invisibility, Invincibility, Shapeshift: '
Strength = "Your Strenth will be overwhelming, your resolve un-wavering. You will be a hammer made of flesh."
Flight = "You will be at home in the sky. You will feel as light as air, able to leap to enormous heights and control your descent with sheer willpower. With mastery wings will form and you will become a creature of the heavens."
Speed = "Time will be your slave. With mastery of this ability, life is controllable."
Invisibility = "Move unseen, unheard and unknown. In time, true mastery will be evident when reality forgets you are there."
Invincibility = "You will not be harmed or negatively affected by any event unless you wish to be harmed and to a specific extent."
Shapeshift = "You can change the way you look and how you sound by willing it to be. With mastery your form can transform into a desired being instead of just impersonation."
PhyPwrDict = {"Strength" : Strength, "Flight" : Flight, "Speed" : Speed, "Invisibility" : Invisibility, "Invincibility" : Invincibility, "Shapeshift" : Shapeshift}
if PwrPri == 'Strength':
print(PwrPri)
print(PhyPwrList[0])
elif PwrPri == 'Flight':
print(PwrPri)
print(PhyPwrList[1])
elif PwrPri == 'Speed':
print(PwrPri)
print(PhyPwrList[2])
elif PwrPri == 'Invisibility':
print(PwrPri)
print(PhyPwrList[3])
elif PwrPri == 'Invincibility':
print(PwrPri)
print(PhyPwrList[4])
elif PwrPri == 'Shapeshift':
print(PwrPri)
print(PhyPwrList[5])
else:
print(Dismiss2)
sys.exit(0)
PhyPwrDict was previously a list from which I would call the index, but I want my code to be more dynamic.
I have researched for about 2 days now and none of the examples I found quite did what I want it to do.
I want whatever is input for PwrPri to be compared against all keys in my PhyPwrDict dictionary and when a key match is found I want the value(a variable containing information the describes the key) to be printed on screen, so if say;
The input of PwrPri = Strength, The PhyPwrDict should be searched for the Strength key and when found print the contents of the Strength variable.
All your if
.. elif
.. else
logic could be replaced with this:
if PwrPri in PhyPwrDict:
print(PhyPwrDict[PwrPri])
else:
print(Dismiss2)
sys.exit(0)
This says, "if the PwrPri
key is in the dictionary, print its corresponding value, else print Dismiss2
and exit".