Back

Activity 1.2 Console Input and Type Conversion

divider

Introduction

Activity 1.2

Console Input and Type Conversion

Topics

  • Using input() to get user input
  • Converting string input to numbers using int() and float()
  • Displaying user input with print()

The input() Function

The input() function pauses the program and waits for the user to type something.

name = input("Enter your name: ")
print("Hello", name)

The input() Function

name = input("Enter your name: ")
print("Hello", name)
Sample
Enter your name: Alex [Enter]
Hello Alex

All Input is a String

By default, input() always returns a string value, even if the user types a number.

age = input("Enter your age: ")
print("You are", age * 12, "months old.")
Note
Enter your age: 35 [enter]
You are 353535353535353535353535 months old.

Issues with Strings and Arithmetic

Because strings aren't treated as numbers in Python, you must convert them before doing arithmetic; otherwise, your calculations may not work as expected.

"3" + "3" # 33

2 * "3" + "3" # 333

Converting Input to Numbers

To perform arithmetic with input, you must convert it to a number using int() or float().

height = float(input("Enter your height in meters: "))
print("Double your height is", height * 2)

Engineering Example

Combine integer and floating-point input for calculations.

count = int(input("Enter the number of bolts needed: "))
cost = float(input("Enter cost per bolt ($): "))
total = count * cost
print(f"Total cost = ${total}")
Sample
Enter the number of bolts needed: 5
Enter cost per bolt ($): 0.75
Total cost = $3.75

int() vs. float()

Use int() when you need a whole number (student ID, age, number of people, etc.).

Use float() when you need a decimal number (dollar amount, weight, GPA, etc.).

Key Terms

input() Function
Pauses the program to accept input from the user as text (string).
Type Conversion
The process of changing a value's data type using functions like int() or float().
int()
Converts a string into an integer (whole number).
float()
Converts a string into a floating-point (decimal) number.

'F' → Fullscreen

Objectives

  • icon Use input() to gather user input.
  • icon Store and display input values using variables.
  • icon Convert string input into numeric data types with int() and float().
  • icon Perform arithmetic using user-provided values.
divider

Activity Tasks

  • icon Create a new project named 1-2-input.py.
  • icon Complete each task individually.

Task 1: Getting User Input

1-2-input.py
# Activity 1.2 - Task 1
# Getting user input
name = input("Enter your name: ")
print("Welcome,", name)

Task 2: Input Strings and Numbers

1-2-input.py
5 collapsed lines
# Activity 1.2 - Task 1
# Getting user input
name = input("Enter your name: ")
print("Welcome,", name)
# Task 2 - Input Strings and Numbers
tool = input("Enter the tool name: ")
price = input("Enter the price: ")
print("The", tool, "costs $", price)

Task 3: Convert Numeric Input

1-2-input.py
12 collapsed lines
# Activity 1.2 - Task 1
# Getting user input
name = input("Enter your name: ")
print("Welcome,", name)
# Task 2 - Input Strings and Numbers
tool = input("Enter the tool name: ")
price = input("Enter the price: ")
print("The", tool, "costs $", price)
# Task 3 - Convert numeric input
radius = float(input("Enter radius of the circle (cm): "))
area = 3.1416 * radius ** 2
print(f"Area = {area} cm^2")

Task 4: Multi-Variable Input (Engineering Example)

1-2-input.py
19 collapsed lines
# Activity 1.2 - Task 1
# Getting user input
name = input("Enter your name: ")
print("Welcome,", name)
# Task 2 - Input Strings and Numbers
tool = input("Enter the tool name: ")
price = input("Enter the price: ")
print("The", tool, "costs $", price)
# Task 3 - Convert numeric input
radius = float(input("Enter radius of the circle (cm): "))
area = 3.1416 * radius ** 2
print(f"Area = {area} cm^2")
# Task 4 - Convert multiple numeric inputs
length = float(input("Enter beam length (m): "))
force = float(input("Enter applied force (N): "))
stress = force / length
print(f"Stress per meter = {stress} N/m")
divider

Sample Output

Your program output should look similar to the sample below.

Sample Output
Enter your name: Maya
Welcome, Maya
Enter radius of the circle (cm): 3
Area = 28.2744 cm^2
Enter beam length (m): 2.5
Enter applied force (N): 500
Stress per meter = 200.0 N/m
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What happens if you forget to convert input before doing arithmetic?
  2. When might it appropriate to use int() instead of float()?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete