Advertisement
GroZnik81

3ta_2012

Dec 20th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. def enroll(h_dict, hero):
  2.     if hero not in h_dict:
  3.         h_dict[hero] = []
  4.     else:
  5.         print(f"{hero} is already enrolled.")
  6.     return h_dict
  7.  
  8.  
  9. def learn(h_dict, hero, spell):
  10.     if hero not in h_dict:
  11.         print(f"{hero} doesn't exist.")
  12.     elif hero in h_dict:
  13.         if spell in h_dict[hero]:
  14.             print(f"{hero} has already learnt {spell}.")
  15.         else:
  16.             heroes_dict[hero].append(spell)
  17.     return h_dict
  18.  
  19.  
  20. def unlearn(h_dict, hero, spell):
  21.     if hero not in h_dict:
  22.         print(f"{hero} doesn't exist.")
  23.     elif hero in h_dict:
  24.         if spell not in h_dict[hero]:
  25.             print(f"{hero} doesn't know {spell}.")
  26.         else:
  27.             heroes_dict[hero].remove(spell)
  28.     return h_dict
  29.  
  30.  
  31. heroes_dict = {}
  32.  
  33. while True:
  34.     command = input().split()
  35.     if command[0] == "End":
  36.         break
  37.  
  38.     action = command[0]
  39.     hero_name = command[1]
  40.     if action == "Enroll":
  41.         heroes_dict = enroll(heroes_dict, hero_name)
  42.  
  43.     elif action == "Learn":
  44.         spell_name = command[2]
  45.         heroes_dict = learn(heroes_dict, hero_name, spell_name)
  46.  
  47.     elif action == "Unlearn":
  48.         spell_name = command[2]
  49.         heroes_dict = unlearn(heroes_dict, hero_name, spell_name)
  50.  
  51. sorted_heroes_dict = dict(sorted(heroes_dict.items(), key=lambda x: ((-len(x[1]), x[0]))))
  52.  
  53. print(f"Heroes:")
  54. for hero, spells in sorted_heroes_dict.items():
  55.     print(f"== {hero}: {', '.join(spells)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement