Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Answers to shopping list, generation, iteration, decoration exercises posted
def shopping_list():
Exercise: shopping list
using while True: and input() create a shopping list interface allowing a user to enter multiple items then type DONE to exit
add a command to delete the last item in the list
add a command to print the current list
add a commands to save the list to a file and load from a file
items = []
def show():
while True:
item = input('What to buy/DONE/DEL/SHOW/SAVE/LOAD? ')
if item == 'DONE':
elif item == 'DEL':
del items[-1]
except IndexError:
print('nothing to delete')
elif item == 'SHOW':
elif item == 'SAVE':
with open('shopping.txt', 'w') as f:
elif item == 'LOAD':
with open('shopping.txt') as f:
items ='\n')
def number_sequence():
Exercise: generation, iteration, decoration
create a generator that produces the sequence: 1, 2, 1, 2, 2, 1, 2, 2, 2, ⋯
n = 1
while True:
yield 1
for i in range(n):
yield 2
n += 1
# NOTE: this generator never exits
def five_from_number_sequence():
write code that will skip the first element of the generator above then print the next 5 elements
seq = number_sequence()
for i in range(5):
def noisy_decorator(fn):
create a decorator that will print all arguments passed to a function and its return value
def wrapper(*args, **kwargs):
print('arguments passed to', fn.__name__, ':', args, kwargs)
rval = fn(*args, **kwargs)
print('return value:', rval)
return rval
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment