
Code
Any Number of Things
Sometimes you don't know how many arguments are coming. A star collects them for you.
*args, extra positional arguments
def shout_all(*words):
for w in words: # words is a tuple
print(w.upper())
shout_all("hi", "yo", "hey")
**kwargs, extra keyword arguments
def show(**info): # info is a dict
for key, value in info.items():
print(key, "=", value)
show(hp=10, gold=3)
The names args and kwargs are just a convention, what matters
is the * and **. You've already used a function like this:
print accepts any number of values!
Assignment
total_loot(*coins)returns the sum of all the numbers passed in (and0if none are passed). Use a loop.make_badge(name, **traits)returns the name followed by each trait askey=value, joined by", ", inside brackets:make_badge("Pip", role="guide", level=3)gives"Pip (role=guide, level=3)". With no traits it returns just"Pip".
total_loot(*coins)returns the sum of all the numbers passed in (and0if none are passed). Use a loop.make_badge(name, **traits)returns the name followed by each trait askey=value, joined by", ", inside brackets:make_badge("Pip", role="guide", level=3)gives"Pip (role=guide, level=3)". With no traits it returns just"Pip".
main.py Python sleeping
Press Run to try your code, then Check to test it.