Python Coding Exercise #11: Food Expenditure
Part of the series Python Practice Exercise — part 11
Before you start: Know input(), int() and float() · Know arithmetic operators and f-strings
The question
Please write a program which estimates a user’s typical food expenditure.
The program asks the user how many times a week they eat at the student cafeteria. Then it asks for the price of a typical student lunch, and for money spent on groceries during the week.
Based on this information the program calculates the user’s typical food expenditure both weekly and daily.
The program should function as follows:
How many times a week do you eat at the student cafeteria? 4
The price of a typical student lunch? 2.5
How much money do you spend on groceries in a week? 28.5
Average food expenditure:
Daily: 5.5 euros
Weekly: 38.5 euros
My Solution
cafeteria_eating = int(input("How many times a week do you eat at the student cafeteria? "))
student_lunch = float(input("The price of a typical student lunch? "))
grocery_spend = float(input("How much money do you spend on groceries in a week? "))
student_lunch_cost = cafeteria_eating * student_lunch
total_weekly_cost = student_lunch_cost + grocery_spend
daily_cost = total_weekly_cost / 7
print(f"""Average food expenditure:
Daily: {daily_cost} euros
Weekly: {total_weekly_cost} euros""")
Output:
Average food expenditure:
Daily: 5.5 euros
Weekly: 38.5 euros
How My Solution Works
1. User input and data types
The program asks for three pieces of information, and each is converted to a number as it’s typed:
cafeteria_eatingusesint(), because the number of lunches is a whole number.student_lunchandgrocery_spendusefloat(), because prices have decimals, like2.5and28.5.
Using the right converter matters. If a user typed 2.5 into an int(), Python would stop with a ValueError. (See Exercise #08 for more on this.)
2. Calculate in small steps
The calculation is split into three lines, each with a descriptive name:
| Step | Calculation | With the sample input |
|---|---|---|
| Cost of cafeteria lunches | cafeteria_eating * student_lunch | 4 × 2.5 = 10.0 |
| Total for the week | student_lunch_cost + grocery_spend | 10.0 + 28.5 = 38.5 |
| Cost per day | total_weekly_cost / 7 | 38.5 / 7 = 5.5 |
The weekly total is divided by 7 because there are seven days in a week, which gives the daily average.
3. A multi-line f-string
The output has three lines, and instead of three print() calls it uses one triple-quoted f-string (see Exercise #04 for f-strings). Triple quotes let a string span several lines, and the line breaks are kept exactly as you type them. The { } slots still work, so the two numbers are inserted where they belong. (More about triple quotes and \n in the strings section of Data Types in Python.)
Watch out for a trailing blank line
It’s tempting to put the closing """ on a line of its own, like this:
print(f"""Average food expenditure:
Daily: {daily_cost} euros
Weekly: {total_weekly_cost} euros
""")
This looks neat, but it prints an extra blank line at the end. The string already ends with a line break (the one before the closing """), and then print() adds its own newline on top. The result is:
Average food expenditure:
Daily: 5.5 euros
Weekly: 38.5 euros
When the output must match exactly, as in this course, the closing """ should sit right after the last character, as in My Solution above.
University Solution
times_in_cafeteria = int(input("How many times a week do you eat at the student cafeteria? "))
price_of_lunch = float(input("The price of a typical student lunch? "))
groceries = float(input("How much money do you spend on groceries in a week? "))
weekly = groceries + times_in_cafeteria * price_of_lunch
print("Average food expenditure:")
print(f"Daily: {weekly / 7} euros")
print(f"Weekly: {weekly} euros")
The University Solution is shorter in two ways. It combines the first two calculation steps into a single line, and it uses three separate print() calls instead of one multi-line string. The output is identical, and because each print() ends the line by itself, there’s no trailing-blank-line trap.
Which is better? Splitting the calculation into named steps, like student_lunch_cost, makes each step easy to read and check. Combining them is quicker to write. Both are correct.
Taking it further: rounding money
With other inputs, the daily figure can have a very long decimal. For example, 3 lunches at 2.6 and 25 euros of groceries gives a weekly total of 32.8 and a daily average of 4.685714285714285. Real money is shown with two decimals, and an f-string can do that with :.2f:
weekly = 32.8
print(f"Daily: {weekly / 7:.2f} euros") # Daily: 4.69 euros
print(f"Weekly: {weekly:.2f} euros") # Weekly: 32.80 euros
The :.2f means “show this as a float with two digits after the decimal point”. It isn’t needed for this exercise, and in fact it would break the match with the sample output (5.5 rather than 5.50), so only use it when the exercise doesn’t dictate the exact format.
Key takeaways
- Use
int()for whole numbers andfloat()for numbers with decimals. - Break a calculation into named steps so each line is easy to read and check.
- Dividing the weekly total by 7 gives the daily average.
- A triple-quoted f-string can print several lines with one
print(). - Put the closing
"""right after the last character, or you get an extra blank line. :.2finside an f-string rounds a number to two decimal places, which is handy for money.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.