Back

Activity 1.8: Console Input and Type Casting

divider

Activity 1.8

Console Input and Type Casting

Key Concepts

Turning Text Into Numbers

int() and float()

The Problem

input() always returns a string — even if the user types a number. Python won't let you do math with a string.

The Problem

Demo
age = input("How old are you? ")
print(age + 1)
Demo
How old are you? 16 [Enter]
TypeError: can only concatenate str (not "int") to str

Type Casting

Wrap a string in int() to convert it to a whole number, so it can be used in math.

Type Casting

Demo
age = input("How old are you? ")
age = int(age)
print(age + 1)
Demo
How old are you? 16 [Enter]
17

Nesting Function Calls

Instead of casting on a separate line, you can nest input() directly inside int() to do it in one step.

Nesting Function Calls

Demo
age = int(input("How old are you? "))
print(age + 1)
Demo
How old are you? 16 [Enter]
17

int() vs. float()

  • Use int() for whole numbers.
  • Use float() for decimal numbers.
  • Typing something that isn't a number, like int('abc'), crashes the program with a ValueError — be sure to type valid numbers when testing.

Three Kinds of Mistakes

Now that programs can crash, it helps to know that not every mistake behaves the same way.

  • Syntax error — Python can't even read your program. Nothing runs at all.
  • Runtime error — it starts running, then crashes partway through. A ValueError is this kind.
  • Logic error — it runs all the way through, prints something, and the something is wrong. There is no error message at all.

Three Kinds of Mistakes

Logic errors are the dangerous ones, because nothing warns you. The only way to catch one is to know what the answer should be and notice that it isn't.

Reading an Error Message

When Python crashes it prints a traceback. It looks like a wall of text, but only part of it matters.

Terminal
Traceback (most recent call last):
File "main.py", line 3, in <module>
age = int(input("How old are you? "))
ValueError: invalid literal for int() with base 10: 'abc'

Reading an Error Message

Terminal
Traceback (most recent call last):
File "main.py", line 3, in <module>
age = int(input("How old are you? "))
ValueError: invalid literal for int() with base 10: 'abc'
  • Read the last line first — it names the error and explains it.
  • The line number tells you where Python noticed the problem — which is not always where you made the mistake.

Today's Objectives

  • Converting input to a number with int() and float()
  • Nesting input() inside a casting function
  • Using converted input in arithmetic

Key Terms

Type Casting
Converting a value from one data type to another.
int()
Converts a value into a whole number.
float()
Converts a value into a decimal number.
Nested Function Call
A function call placed inside another function call, evaluated from the inside out.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-8-type-casting.

Note: since we haven't learned how to handle input errors yet, make sure you type actual numbers when testing your program. Typing letters into int() or float() will crash your program.


Task 1: Age in Months

  • Ask the user for their age, cast it to an int.
  • Multiply it by 12 and print the result in months using an f-string.
Console Input and Type Casting
age = int(input("How old are you? "))
months = age * 12
print(f"You are approximately {months} months old.")
#

Task 2: Rectangle Area

  • Ask the user for a rectangle's length and width, casting each to a float.
  • Multiply them together and print the area using an f-string.
Console Input and Type Casting
age = int(input("How old are you? "))
months = age * 12
print(f"You are approximately {months} months old.")
length = float(input("What is the length of the rectangle? "))
width = float(input("What is the width of the rectangle? "))
area = length * width
print(f"The area of the rectangle is {area}.")
#

Task 3: Quiz Average

  • Ask the user for two quiz scores, casting each to an int.
  • Print their average using an f-string.
Console Input and Type Casting
age = int(input("How old are you? "))
months = age * 12
print(f"You are approximately {months} months old.")
length = float(input("What is the length of the rectangle? "))
width = float(input("What is the width of the rectangle? "))
area = length * width
print(f"The area of the rectangle is {area}.")
score1 = int(input("Enter your first quiz score: "))
score2 = int(input("Enter your second quiz score: "))
average = (score1 + score2) / 2
print(f"Your average quiz score is {average}.")
#

Challenge (Optional): Hours to Minutes

  • Ask the user for a number of hours, cast it appropriately, and print the equivalent number of minutes using an f-string.
divider

Checkpoint

Verify your program works correctly.

Example Output
How old are you? 16 [Enter]
You are approximately 192 months old.
What is the length of the rectangle? 4.5 [Enter]
What is the width of the rectangle? 2 [Enter]
The area of the rectangle is 9.0.
Enter your first quiz score: 88 [Enter]
Enter your second quiz score: 94 [Enter]
Your average quiz score is 91.0.
divider

Reflection

Answer the following questions before submitting your work.

  1. What happens if you try to add 1 directly to the result of input() without casting it first?
  2. When would you choose int() over float(), or vice versa?
  3. What do you think would happen if a user typed a word instead of a number into one of your programs? What might a program want to do instead of crashing?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete