2015-03-16 3 views
0

Это мой первый вопрос, задающий вопрос здесь, поэтому, если я что-то оставлю, простите меня.python 3.4 - вход, ключ соответствия, значение печати

Я начинающий программист, и я начинаю с python 3.4.

У меня есть функция (ниже), которую я создал, и она отлично работает при выполнении, но я пытаюсь сжать свой код.

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 ранее был список, из которого я назвал бы индекс, но я хочу, чтобы мой код, чтобы быть более динамичным.

Я исследовал около 2 дней, и ни один из примеров, которые я нашел, не сделал то, что я хочу.

Я хочу, чтобы все, что было введено для PwrPri, сравнивалось со всеми ключами в словаре PHyPwrDict, и когда найдено совпадение ключей, я хочу, чтобы значение (переменная, содержащее информацию, описывающую ключ) печаталось на экране, поэтому, если сказать;

Вход PwrPri = Strength, The PhyPwrDict следует искать ключом Strength и при обнаружении распечатать содержимое переменной Strength.

ответ

0

Все ваши if .. elif .. else логика может быть заменена следующим образом:

if PwrPri in PhyPwrDict: 
    print(PhyPwrDict[PwrPri]) 
else: 
    print(Dismiss2) 
    sys.exit(0) 

Это говорит о том, «если ключ PwrPri в словаре, распечатайте его соответствующее значение, иначе печать Dismiss2 и выход ».

+0

Это был именно то, что я искал, спасибо youu !!. – DaNNuN