These are the notes for Python episode 3. You will learn how a program asks the user for data with input(), why "3" * 12 prints 333333333333, why input() always returns a string, converting values with int(), float(), str() and bool(), the ValueError, why int() chops while round() rounds, implicit vs explicit conversion, and reading two numbers from one line with split(). Every example below runs as-is in the SyntaxPilot compiler (type the input in the input box before you press Run), with its real output from Python 3.13.
The problem: the cups are fixed in the code
Last episode's bill had the number of cups written inside the code:
cups = 3
print("Bill:", cups * 12)
Every customer orders a different number. Raju can't open the code and edit it for each customer: the program should ask.
input(): asking the user
input() works like a waiter taking your order:
| At the chai stall | In Python |
|---|---|
| Waiter asks "How many cups?" | input("How many cups? ") shows the prompt |
| Waits, pen ready | the program pauses until the user presses Enter |
| Writes your answer on a slip | whatever was typed is returned, and we store it: cups = input(...) |
Put a space at the end of the prompt so the user's answer doesn't stick to the question mark.
The 333333 bug
cups = input("How many cups? ")
print("Bill:", cups * 12)
print(type(cups))
Input: 3
How many cups? Bill: 333333333333
<class 'str'>
(In the SyntaxPilot compiler the typed input isn't echoed, so the prompt and the output appear on one line.)
Twelve threes! cups is the text "3", not the number 3, and a string times a number repeats the string.
Why it failed: a keyboard sends characters, not numbers.
input()always returns astr, even when the user types digits. Before doing maths, convert the text into a number.
Type conversion with int()
Type conversion (or type casting) means turning a value of one type into another type.
cups = int(input("How many cups? "))
print("Bill:", cups * 12)
print(type(cups))
Input: 3
How many cups? Bill: 36
<class 'int'>
Python works from the inside out: input() returns "3", then int("3") gives the number 3. With input 5 the same program prints Bill: 60, with no code changes.
Forget to convert before adding, and you get an error instead:
age = input("Age? ")
print(age + 5)
Input: 19
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(age + 5)
~~~~^~~
TypeError: can only concatenate str (not "int") to str
Fix: age = int(input("Age? ")), and age + 5 is 24.
ValueError: right type, wrong value
milk = int(input("Milk in litres? "))
print("Double:", milk * 2)
Input: 2.5
Milk in litres? Traceback (most recent call last):
File "main.py", line 1, in <module>
milk = int(input("Milk in litres? "))
ValueError: invalid literal for int() with base 10: '2.5'
A literal is a value written out as text. int() reads whole numbers only, so "2.5" can't become an int. The type was right (a string) but the value inside was wrong for int(): that's what ValueError means. "Base 10" just means ordinary decimal digits.
For decimals, use float():
milk = float(input("Milk in litres? "))
print("Double:", milk * 2)
Input: 2.5
Milk in litres? Double: 5.0
Words fail too:
cups = int(input("How many cups? "))
Input: three
ValueError: invalid literal for int() with base 10: 'three'
int() only understands digits. (In the exceptions episode you'll learn to catch this with try/except and ask again.)
Rule of thumb: int() for counts (cups, people, age), float() for measurements and money (litres, rupees, marks with decimals).
int() chops, round() rounds
print(int(3.9))
print(round(3.9))
print(int(" 5 "))
print(int("007"))
3
4
5
7
int()on a float simply cuts off the decimal part, like a knife:3.9 → 3. It does not round.round()goes to the nearest whole number:3.9 → 4(or to given decimal places:round(2.345, 1)).int()ignores spaces around the digits and leading zeros.int("12.0")is aValueError(the text has a decimal point), butint(float("12.0"))works.
str(): numbers into text
total = 36
print("Total: " + str(total))
print(str(3.5), float(7), int(True))
Total: 36
3.5 7.0 1
str() fixes the "text + number" TypeError. And True converts to 1, False to 0.
bool(): is there anything in it?
print(bool(0), bool(5))
print(bool(""), bool("hi"))
print(bool("False"))
False True
False True
True
| Value | bool() |
Why |
|---|---|---|
0, 0.0 |
False |
zero is "empty" |
"" (empty string) |
False |
nothing inside |
None |
False |
no value |
5, -1, 0.1 |
True |
any non-zero number |
"hi", " " |
True |
any non-empty string |
"False" |
True |
it's a 5-letter string, not the boolean False |
Like a jar with a note saying "empty" in it: it isn't empty. To check what a user typed, compare the text (answer == "yes"), which comes with operators in episode 4.
Implicit vs explicit conversion
print(3 + 2.5)
print(type(3 + 2.5))
print(True + 1)
5.5
<class 'float'>
2
- Implicit conversion (automatic): when you mix
intandfloat, Python widens the int to a float so nothing is lost.boolacts like0/1in maths. - Explicit conversion (by you):
int(),float(),str(),bool(). - Python never converts between text and numbers on its own. That's always your decision.
Two numbers on one line: split()
a, b = input("Two numbers: ").split()
print(a + b)
print(int(a) + int(b))
Input: 4 5
Two numbers: 45
9
.split() cuts the text at spaces into pieces ("4" and "5"), which we catch in a and b. They are still strings, so a + b joins them. split() cuts, it doesn't convert. (If the user types three numbers, a, b = ... fails with ValueError: too many values to unpack (expected 2); lists in episode 10 handle any count.)
Common mistakes (and the real error messages)
| Mistake | Error | Why | Fix |
|---|---|---|---|
input("Cups? ") * 12 |
no error: "3" * 12 → 333333333333 |
input() always returns a str |
int(input("Cups? ")) * 12 |
input("Age? ") + 5 |
TypeError: can only concatenate str (not "int") to str |
text + number | int(input("Age? ")) + 5 |
int("2.5") |
ValueError: invalid literal for int() with base 10: '2.5' |
int() reads whole numbers only |
float("2.5") |
int("three") |
ValueError: invalid literal for int() with base 10: 'three' |
not digits | ask for digits (and later, try/except) |
int(3.9) expecting 4 |
no error: 3 |
int() chops |
round(3.9) |
bool("False") expecting False |
no error: True |
non-empty string | compare the text |
"Total: " + 36 |
TypeError: can only concatenate str (not "int") to str |
text + number | "Total: " + str(36) or a comma |
Mini project: bill splitter
# Mini project: bill splitter
name = input("Your name: ")
total = float(input("Total bill: "))
people = int(input("How many friends? "))
share = total / people
print()
print("Hi", name + "!")
print("Bill:", total, "for", people, "people")
print("Each pays:", round(share, 2))
Input (one answer per line):
Ravi
1000
3
Output:
Your name: Total bill: How many friends?
Hi Ravi!
Bill: 1000.0 for 3 people
Each pays: 333.33
total is a float because bills can have paise; people is an int because you can't have 2.5 friends. The name needs no conversion: it's text.
Practice
- Ask for a name and birth year, and print the age in 2026.
Solution
name = input("Name: ")
year = int(input("Birth year: "))
print(name, "is", 2026 - year, "years old in 2026")
Input Meera and 2007 → Meera is 19 years old in 2026
- Celsius to Fahrenheit:
F = C * 9 / 5 + 32.
Solution
c = float(input("Celsius: "))
f = c * 9 / 5 + 32
print("Fahrenheit:", round(f, 1))
Input 37 → Fahrenheit: 98.6
- Extend the bill splitter with a tip percentage.
Solution
total = float(input("Total bill: "))
tip = float(input("Tip %: "))
people = int(input("People: "))
grand = total + total * tip / 100
print("Each pays:", round(grand / people, 2))
Input 1000, 10, 4 → Each pays: 275.0
- Read two numbers on one line and print their sum and product.
Solution
a, b = input().split()
a, b = int(a), int(b)
print("Sum:", a + b)
print("Product:", a * b)
Input 6 7 → Sum: 13 and Product: 42
Quick quiz
The user types 5. What prints?
x = input()
print(x * 2, int(x) * 2)
A) 10 10 B) 55 10 C) 55 55 D) Error
Answer
B) 55 10. input() gives the string "5", so x * 2 repeats it; int(x) is the number 5, so int(x) * 2 is 10.
Interview questions
- What type does
input()return? Alwaysstr, whatever the user types. - Difference between
int(3.9)andround(3.9)?int()truncates toward zero (3, andint(-3.9)is-3);round()rounds to the nearest (4). Python'sround()uses banker's rounding for exact halves:round(2.5)is2. - What is the difference between
TypeErrorandValueError?TypeError: the operation doesn't support that type ("a" + 1).ValueError: the type is fine but the value isn't (int("abc")). - What is implicit type conversion? Python automatically converting a value, such as
inttofloatin3 + 2.5. - Why is
bool("False")True? Every non-empty string is truthy; only""is falsy.
Glossary
| Term | Meaning | Example |
|---|---|---|
| Input | Data the user gives the program while it runs | typing 3 |
input() |
Shows a prompt, waits for Enter, returns the typed text as str |
input("Cups? ") |
| Prompt | The question input() shows |
"How many cups? " |
| Type conversion / type casting | Changing a value from one type to another | int("3") → 3 |
int() |
Converts to a whole number (chops decimals) | int("42"), int(3.9) → 3 |
float() |
Converts to a decimal number | float("2.5") |
str() |
Converts to text | str(36) → "36" |
bool() |
Converts to True/False; empty/zero is False |
bool("") → False |
| Literal | A value written out as text in code or input | '2.5' |
| ValueError | Right type, but a value the function can't use | int("2.5") |
| TypeError | An operation on types that don't fit | "19" + 5 |
| Truncate | Cut off the decimal part | int(3.9) → 3 |
round() |
Round to the nearest (or to n decimals) | round(3.9) → 4 |
| Implicit conversion | Python converts automatically | 3 + 2.5 → 5.5 |
| Explicit conversion | You convert with a function | float(7) |
split() |
Cuts a string at spaces into pieces | "4 5".split() |
| Truthy / falsy | Values bool() treats as True / False |
"hi" / 0 |
Summary
input("prompt")asks the user and waits; it always returns astr.- Convert with
int()for counts andfloat()for decimals before doing maths. ValueError= the right type with a value the function can't read (int("2.5"),int("three")).int()chops,round()rounds.str()turns numbers into text;bool()isFalseonly for empty things (0,"",None), sobool("False")isTrue.- Python mixes
intandfloatby itself, but never text and numbers. input().split()reads several values from one line; they are still strings.
Next episode: operators: arithmetic, comparison, logical and assignment.