Python Coding Exercise #09: Calculations with Input
Part of the series Python Practice Exercise — part 9
Before you start: Know input() and int() · Know arithmetic operators and f-strings
This post groups three short exercises with the same shape: ask the user for numbers with input(), convert them with int(), calculate, and print the result. If int(input(...)) looks unfamiliar, Exercise #08 explains why the conversion is needed.
Exercise 1: Seconds in a day
The question
Please write a program which asks the user for a number of days. The program then prints out the number of seconds in the amount of days given.
How many days? 1
Seconds in that many days: 86400
Another example:
How many days? 7
Seconds in that many days: 604800
My Solution
days = int(input("How many days? "))
print(f"Seconds in that many days: {days * 60 * 60 * 24}")
How it works
One day has 24 hours, each hour has 60 minutes, and each minute has 60 seconds. So the seconds in one day are 60 * 60 * 24 = 86400, and the seconds in any number of days are that number multiplied by days. For 7 days, that’s 7 * 86400 = 604800.
The multiplication happens inside the f-string braces (f-strings were introduced in Exercise #04), and the order of the numbers doesn’t matter: days * 24 * 60 * 60 gives the same result. The int() is needed because input() returns text, and you can’t do arithmetic on text. (A similar calculation appeared in Exercise #03, where we worked out the minutes in a year.)
University Solution
how_many = input("How many days? ")
days = int(how_many)
seconds = days * 24 * 60 * 60
print(f"Seconds in that many days: {seconds}")
The University Solution takes smaller steps: it stores the typed text, converts it to an integer, calculates the seconds into a variable, and then prints. It’s a few lines longer, but each step has a name, which makes it easy to follow and easy to debug.
Exercise 2: Sum and product
The question
Please write a program which asks the user for two numbers. The program will then print out the sum and the product of the two numbers.
Number 1: 3
Number 2: 7
The sum of the numbers: 10
The product of the numbers: 21
My Solution
number1 = int(input("Number 1: "))
number2 = int(input("Number 2: "))
print(f"The sum of the numbers: {number1+number2}")
print(f"The product of the numbers: {number1*number2}")
How it works
Each number goes into its own variable, number1 and number2. Then two f-strings calculate number1+number2 (3 + 7 = 10) and number1*number2 (3 × 7 = 21) inside the curly braces.
University Solution
number1 = int(input('Number 1: '))
number2 = int(input('Number 2: '))
print("The sum of the numbers:", number1 + number2)
print("The product of the numbers:", number1 * number2)
The University Solution uses commas instead of f-strings. It prints the same output, because print() automatically puts a space between comma-separated values, and the sample output happens to want a space after each colon. (In Exercise #05 we saw those automatic spaces cause trouble, but here they are exactly what’s needed.)
Exercise 3: Sum and mean
The question
Please write a program which asks the user for four numbers. The program then prints out the sum and the mean of the numbers.
Number 1: 2
Number 2: 1
Number 3: 6
Number 4: 7
The sum of the numbers is 16 and the mean is 4.0
My Solution
number1 = int(input("Number 1: "))
number2 = int(input("Number 2: "))
number3 = int(input("Number 3: "))
number4 = int(input("Number 4: "))
total = number1 + number2 + number3 + number4
mean = total / 4
print(f"The sum of the numbers is {total} and the mean is {mean}")
How it works
The sum is all four numbers added together: 2 + 1 + 6 + 7 = 16. The mean (the average) is the sum divided by how many numbers there are: 16 / 4 = 4.0.
Note:
- The mean prints as
4.0, not4. The/operator always returns a float, even when the division is exact, so the sample output shows4.0. (See Exercise #06 for more on this.)
University Solution
number1 = int(input('Number 1: '))
number2 = int(input('Number 2: '))
number3 = int(input('Number 3: '))
number4 = int(input('Number 4: '))
sum = number1 + number2 + number3 + number4
print(f"The sum of the numbers is {sum} and the mean is {sum/4}")
Here the mean is calculated inside the f-string instead of being stored first. It also names the variable sum, which works, but it’s worth avoiding. sum is already the name of a built-in Python function that adds up a list of numbers, and creating your own variable called sum replaces it:
sum = 16
sum([1, 2])
TypeError: 'int' object is not callable
After sum = 16, Python no longer has the built-in sum() in that program, so calling it fails. Picking a different name such as total avoids the problem entirely. (This comes up again in Exercise #50 and Exercise #53, and sum() is explained in Built-in Functions in Python.)
Key takeaways
input()returns text, so wrap it inint()before calculating.- You can calculate directly inside an f-string’s braces, or store the result in a variable first. Both are correct.
- Commas in
print()add a space automatically. f-strings add only the spaces you type. - The mean is the sum divided by the count, and
/always gives a float (16 / 4is4.0). - Don’t name a variable after a built-in function such as
sum, because it hides the function.
✦ 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: These exercises are 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.