4 of 6
You're looting treasure chests in a dungeon. Each item has a name and a gold value. You only want valuable items (worth 10 gold or more), and your bag can only hold 50 gold worth of loot before it's full.
Use continue to skip cheap items and break to stop when your bag is full.
loot = [("sword", 25), ("potion", 5), ("shield", 30), ("arrow", 3), ("helmet", 20), ("ring", 50)] bag_limit = 50
Expected output:
Looted sword (25 gold) - Bag: 25/50
Skipped potion (5 gold) - not valuable enough
Bag full! Looted shield but bag is now 55/50
Stopped looting - bag is full!
Total loot: 55 gold (2 items)
for loop to go through the loot listcontinue to skip items worth less than 10 gold (print a skip message first)breakEach item in the loot list is a tuple with two values. You can unpack them in the for loop:
for name, value in loot:
Inside the loop:
continuebreakWhen the bag becomes full, you print a different message than the normal loot message. You'll need an if/else after adding the item:
if bag_total >= bag_limit: print(f"Bag full! Looted {name} but bag is now {bag_total}/{bag_limit}") print("Stopped looting - bag is full!") break else: print(f"Looted {name} ({value} gold) - Bag: {bag_total}/{bag_limit}")
β Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect
β Functions, classes, recursion, print()
β No file system, subprocess, OS access, or network requests
β No pip install (all supported modules are pre-loaded)
β±οΈ 5 second execution time limit