Python Coding Exercise #08: Converting Input to Integer
Part of the series Python Practice Exercise — part 8
Before you start: Know input() and f-strings · Know basic arithmetic operators
Both exercises in this post follow the same pattern: ask the user for a number, convert it to an integer, and then do arithmetic with it. The conversion is the whole point, and it connects directly to Exercise #01, where input() returned text and gave us 2555 instead of 17.
Why the conversion is needed
input() always returns a string, even when the user types digits. Arithmetic on a string doesn’t do what you expect, so before calculating you convert the text to a number with int():
text = "3"
number = int(text)
print(type(text)) # <class 'str'>
print(type(number)) # <class 'int'>
(More on types and conversion in Data Types in Python.)
Exercise 1: Times five
The question
Please write a program which asks the user for a number. The program then prints out the number multiplied by five.
Please type in a number: 3
3 times 5 is 15
My Solution
number = int(input("Please type in a number: "))
print(f"{number} times 5 is {number * 5}")
How it works
input("Please type in a number: ")shows the question and returns what the user typed, as a string such as"3".int(...)wraps around it and converts that string to the integer3. Doing both in one line is a very common pattern.- The f-string inserts the number, and
{number * 5}calculates the answer right inside the braces.
What happens without int()
If you leave out int(), this is what the program does:
number = input("Please type in a number: ")
print(f"{number} times 5 is {number * 5}")
Please type in a number: 3
3 times 5 is 33333
Because number is the string "3", the * operator repeats it five times instead of multiplying. There’s no error, which makes this mistake easy to miss. (Repeating strings with * is the subject of Exercise #54.)
University Solution
The University Solution converts the number only at the moment it calculates:
number = input("Please type in a number: ")
result = int(number) * 5
print(f"{number} times 5 is {result}")
Here number stays a string, and int(number) is used just for the multiplication. The result is stored in a variable called result, which makes the last line easy to read.
Both programs give the same output for 3. There is one small difference. If the user types 03, My Solution prints 3 times 5 is 15, because the integer 3 has no leading zero. The University Solution prints 03 times 5 is 15, because it displays exactly what was typed. For this exercise either is fine.
Exercise 2: Name and age
The question
Please write a program which asks the user for their name and year of birth. The program then prints out a message as follows:
What is your name? Frances Fictitious
Which year were you born? 1990
Hi Frances Fictitious, you will be 31 years old at the end of the year 2021
My Solution
name = input("What is your name? ")
year_born = int(input("Which year were you born? "))
print(f"Hi {name}, you will be {2021-year_born} years old at the end of the year 2021")
How it works
namestays a string, since it’s only inserted into the message.year_bornis converted withint()because we need to subtract it from 2021.- The calculation
2021-year_bornhappens inside the f-string braces: 2021 − 1990 = 31.
University Solution
The University Solution stores the age in its own variable first:
name = input("What is your name? ")
born = int(input("Which year were you born? "))
age = 2021 - born
print(f"Hi {name}, you will be {age} years old at the end of the year 2021")
It gives the same output. Giving the calculation its own line and name (age) makes the final print() easier to read, especially once programs get longer.
What happens without int()
If you forget the conversion here, you get an error:
born = input("Which year were you born? ")
age = 2021 - born
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Python is telling you it can’t subtract a string from a number. Whenever you see an error like this after an input(), check whether you forgot to convert.
Things to note
int() only works on text that looks like a whole number. Anything else stops the program with a ValueError:
Please type in a number: 3.5
ValueError: invalid literal for int() with base 10: '3.5'
Please type in a number: abc
ValueError: invalid literal for int() with base 10: 'abc'
If you want to allow decimals, use float() instead:
number = float(input("Please type in a number: "))
print(f"{number} times 5 is {number * 5}") # with 3.5: 3.5 times 5 is 17.5
One more detail about the second exercise: the year 2021 is typed directly into the program, because that’s what the exercise asks for. In a real program you would normally get the current year from Python instead:
from datetime import date
this_year = date.today().year
Key takeaways
input()always returns a string, even when the user types a number.- Use
int()to convert it to a whole number before doing arithmetic. - Forgetting
int()either repeats the text ("3" * 5gives33333) or raises aTypeError, depending on the operation. int(input(...))converts the answer as soon as it’s typed. Converting later, as inint(number) * 5, keeps the original text for printing.int()can’t convert decimals or words. Usefloat()for decimal numbers.- Putting a calculation on its own line, such as
age = 2021 - born, makes the finalprint()easier to read.
✦ 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.