
Code
enumerate & zip
Code like for i in range(len(items)): works, but Python has nicer tools.
enumerate gives you a counter and the item:
for i, name in enumerate(["Ada", "Bo"], start=1):
print(i, name) # 1 Ada / 2 Bo
zip walks several sequences side by side:
islands = ["Moss", "Ash"]
keepers = ["Ada", "Bo"]
for island, keeper in zip(islands, keepers):
print(f"{keeper} guards {island}")
dict(zip(islands, keepers)) # {'Moss': 'Ada', 'Ash': 'Bo'}
Careful: zip silently stops at the shortest input. If the lengths
should match, use zip(a, b, strict=True), which raises ValueError
when they don't, so mistakes can't hide.
Assignment
leaderboard(names)returns a list of strings numbered from 1:["1. Ada", "2. Bo", ...].pair_up(islands, keepers)returns a dict mapping each island to its keeper. If the lists have different lengths it must raiseValueError.
leaderboard(names)returns a list of strings numbered from 1:["1. Ada", "2. Bo", ...].pair_up(islands, keepers)returns a dict mapping each island to its keeper. If the lists have different lengths it must raiseValueError.
main.py Python sleeping
Press Run to try your code, then Check to test it.