DiYane

Treasure Hunt

Aug 10th, 2023
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. treasure_chest = input().split('|')
  2.  
  3. command_input = input()
  4. stolen_list = []
  5. while command_input != 'Yohoho!':
  6.     command_args = command_input.split()
  7.     command = command_args[0]
  8.     if command == 'Loot':
  9.         items = command_args[1:]
  10.         for item in items:
  11.             if item not in treasure_chest:
  12.                 treasure_chest.insert(0, item)
  13.     elif command == 'Drop':
  14.         index = int(command_args[1])
  15.         if index < 0 or index >= len(treasure_chest):
  16.             command_input = input()
  17.             continue
  18.         removed_item = treasure_chest.pop(index)
  19.         treasure_chest.append(removed_item)
  20.     elif command == 'Steal':
  21.         number = int(command_args[1])
  22.         if number >= len(treasure_chest):
  23.             number = len(treasure_chest)
  24.             idx = 0
  25.         else:
  26.             idx = len(treasure_chest) - number
  27.         stolen_list = treasure_chest[idx:idx+number:]
  28.         del treasure_chest[idx:idx+number:]
  29.         if any(stolen_list):
  30.             print(', '.join(stolen_list))
  31.  
  32.     command_input = input()
  33.  
  34. length = [len(s) for s in treasure_chest]
  35. if len(treasure_chest) > 0:
  36.     sum_length = sum(length) / len(treasure_chest)
  37.     print(f'Average treasure gain: {sum_length:.2f} pirate credits.')
  38. else:
  39.     print('Failed treasure hunt.')
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment