A return statement finishes a function and sends a value back to the code that called it. Variables created inside the function stay there, so returning a value lets the main program use it. section 3.0
A coffee-shop choice
A menu function turns the user’s number into an item, then returns that item: return answer. The main program stores the result with choice = menu(drinks, ...) and can print coffee, vanilla, or another selection. section 3.0
def menu(choices, title, prompt):
print(title)
print("--------------------------------")
i = 1
for c in choices:
print(i, c)
i = i + 1
choice = input(prompt)
answer = choices[int(choice) - 1]
return answer
See the complete menu function: it reads input, converts the selection, retrieves the chosen list item, and returns answer. section 3.0
Returning the wrong value can produce a misleading result. Returning {} gives the caller an empty dictionary; returning order passes back the customer’s actual name and order data. section 7.0
YOUR TURN
- Write a get_order() function, collect a customer’s name, return the order dictionary, and test the result in the main program. section 7.0




