Python Coding Exercise #14: Soup or No Soup
Part of the series Python Practice Exercise — part 14
Before you start: Know input(), int() and f-strings · Know if and else
The question
Please write a program which asks for the user’s name. If the name is anything but “Jerry”, the program then asks for the number of portions and prints out the total cost. The price of a single portion is 5.90.
Two examples of the program’s execution:
Please tell me your name: Kramer
How many portions of soup? 2
The total cost is 11.8
Next please!
Please tell me your name: Jerry
Next please!
My Solution
name = input("Please tell me your name: ")
if name == "Jerry":
print("Next please!")
else:
portions = int(input("How many portions of soup? "))
print(f"The total cost is {round(portions * 5.90, 2)}\nNext please!")
How My Solution Works
1. Compare the name
name == "Jerry" checks whether the typed text is exactly Jerry. Comparing strings with == works just like comparing numbers.
2. Two branches
ifbranch (the name is Jerry): Jerry gets no soup, so the program only printsNext please!. It never asks for a number of portions.elsebranch (anyone else): The program asks for the portions, converts the answer withint(), calculates the cost, and prints the total followed byNext please!.
An important detail: the question “How many portions of soup?” sits inside the else. So for Jerry it is never asked, and that’s why his run has only two lines.
3. Calculating the cost
The cost is the number of portions times the price: portions * 5.90. With 2 portions, that is 2 * 5.90 = 11.8.
4. Why round() is there
Floating-point numbers can pick up tiny errors, as we saw with 0.1 + 0.2 in Data Types in Python, Exercise #17 and Exercise #31. Money is a place where that shows up. With 3 portions:
print(5.9 * 3) # 17.700000000000003
print(round(5.9 * 3, 2)) # 17.7
round(value, 2) rounds to two decimal places, which cleans up the noise and prints 17.7, just as a price should look.
5. Printing two lines at once
The \n inside the f-string starts a new line, so one print() outputs both The total cost is ... and Next please!. (There’s more about \n in the strings section of Data Types in Python.)
University Solution
name = input("Please tell me your name: ")
if name != "Jerry":
portions = int(input("How many portions of soup? "))
cost = 5.9 * portions
print("The total cost is", cost)
print("Next please!")
The University Solution turns the logic around in two ways:
- It uses
!=(“is not equal to”). Only a customer who is not Jerry gets asked about portions, so there’s noelseat all. Jerry’s case is simply the one where theifis skipped. print("Next please!")is outside theif. It is not indented, so it runs for everyone, whatever the name. That’s why the line only needs to be written once. In My Solution, “Next please!” is written in both branches.
For input that comes out cleanly, such as Kramer with 2 portions, both programs print the same output. The one difference is rounding. This version prints the raw result of 5.9 * portions, so for 3 portions it shows 17.700000000000003 instead of 17.7. Adding round() fixes that:
cost = round(5.9 * portions, 2)
Which one is better? The University Solution avoids repeating Next please!, and My Solution reads very clearly with its two branches. Combining the two ideas, != with the final print() outside the if, plus round(), gives the tidiest program.
A detail about capital letters
String comparison is case-sensitive. "Jerry" and "jerry" are different strings:
print("Jerry" == "jerry") # False
print("Jerry".lower() == "jerry".lower()) # True
So if someone typed jerry in lowercase, the program would treat them as an ordinary customer and ask about soup. The exercise only needs the exact name Jerry, but .lower() is a handy trick when you want to ignore capitalisation. It is one of many string methods, explained in String Methods in Python.
Key takeaways
- Use
==to compare strings, and!=to check that they differ. - Code inside an
iforelseis indented. Code with no indentation after it runs in every case. - Writing a line once, outside the
if, is better than writing it in both branches. round(value, 2)tidies up floating-point noise, which is especially useful for prices.- Comparing strings is case-sensitive.
"Jerry"is not the same as"jerry".
✦ 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.