Advertisement
Guest User

edited

a guest
Aug 20th, 2020
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. cities = {}
  2.  
  3. command = input()
  4.  
  5. while command != "Sail":
  6.     tokens = command.split("||")
  7.     town = tokens[0]
  8.     people = int(tokens[1])
  9.     gold = int(tokens[2])
  10.  
  11.     if town not in cities.keys():  # town not in cities (същото е)
  12.         cities[town] = [people, gold]
  13.     else:
  14.         cities[town][0] += people
  15.         cities[town][1] += gold
  16.     command = input()
  17.  
  18. command = input()
  19. while command != "End":
  20.     tokens = command.split("=>")
  21.     cmd = tokens[0]
  22.  
  23.     if cmd == "Plunder":
  24.         town = tokens[1]
  25.         people = int(tokens[2])
  26.         gold = int(tokens[3])
  27.  
  28.         # if cities[town][0] - people > 0 and cities[town][1] - gold > 0:
  29.         #     cities[town][0] -= people
  30.         #     cities[town][1] -= gold
  31.         #
  32.         # elif cities[town][0] - people < 0 or cities[town][1] - gold < 0:
  33.         #     if cities[town][0] - people < 0:
  34.         #         people = cities[town][0]
  35.         #         cities[town][0] = 0
  36.         #
  37.         #     if cities[town][1] - gold < 0:
  38.         #         gold = cities[town][1]
  39.         #         cities[town][1] = 0
  40.         #
  41.         #     cities.pop(town)
  42.         #     print(f'{town} has been wiped off the map!')
  43.  
  44.         cities[town][0] -= people
  45.         cities[town][1] -= gold
  46.         print(f'{town} plundered! {gold} gold stolen, {people} citizens killed.')
  47.  
  48.         if cities[town][0] <= 0 or cities[town][1] <= 0:
  49.             cities.pop(town)
  50.             print(f'{town} has been wiped off the map!')
  51.  
  52.     elif cmd == "Prosper":
  53.         town = tokens[1]
  54.         gold = int(tokens[2])
  55.  
  56.         if gold < 0:
  57.             print(f'Gold added cannot be a negative number!')
  58.         else:
  59.             cities[town][1] += gold
  60.             print(f'{gold} gold added to the city treasury. {town} now has {cities[town][1]} gold.')
  61.     command = input()
  62.  
  63. if len(cities) > 0:  # if cities (същото е)
  64.  
  65.     print(f'Ahoy, Captain! There are {len(cities)} wealthy settlements to go to:')
  66.  
  67.     # for key, value in sorted(cities.items(), key=lambda x: (x[1], x), reverse=True):
  68.     # не достъпваш правилните елементи и мисля, че reverse=True се прилага и върху двете
  69.     # на теб ти трябва само едното да е descending order
  70.     for key, value in sorted(cities.items(), key=lambda x: (-x[1][1], x[0])):
  71.         print(f'{key} -> Population: {value[0]} citizens, Gold: {value[1]} kg')
  72.  
  73. else:
  74.     print(f'Ahoy, Captain! All targets have been plundered and destroyed!')
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement