Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list_initial_loot = input().split('|')
- # no need to capture input first and then split, lines 1 & 3 can be combined in initial_loot = input().split('|')
- command = input()
- while command != "Yohoho!":
- tokens = command.split(" ")
- comanda = tokens[0]
- if comanda == "Loot":
- for k in tokens[1:]: # could be simplified by for k in tokens[1:]
- if k not in list_initial_loot: # could skip the check for k != 'Loot' if above statement is included in the for cycle
- list_initial_loot.insert(0, k)
- elif comanda == "Drop":
- index = int(tokens[1])
- if 0 <= index < len(list_initial_loot): # index CAN be 0 , should be 0 <= index < len(list_initial_loot)
- list_initial_loot.append(list_initial_loot.pop(index)) # could be simplified to initial_loot.append(initial_loot.pop(index))
- elif comanda == "Steal":
- counta = int(tokens[1])
- stolen = [] # double declaration of stolen [] , not necessary
- if counta <= len(list_initial_loot): # no need for count to be > 0 , it could also be == to len(initial list) or even > len(list)
- stolen = list_initial_loot[-counta::]
- for i in range(counta):
- list_initial_loot.pop()
- else:
- stolen = list_initial_loot.copy()
- list_initial_loot.clear()
- print(", ".join(stolen))
- command = input()
- if list_initial_loot: # could be simplified to if list_initial_loot
- sum_of_items = 0
- for item in list_initial_loot:
- sum_of_items += len(item)
- averageGain = sum_of_items / len(list_initial_loot)
- print(f"Average treasure gain: {averageGain:.2f} pirate credits.")
- else:
- print("Failed treasure hunt.")
Advertisement
Add Comment
Please, Sign In to add comment