Back

Activity 1.7: Console Input

divider

Activity 1.7

Console Input

Key Concepts

Getting Information From the User

The input() function

The input() Function

input() displays a message, waits for the user to type something and press Enter, then returns whatever they typed.

The input() Function

Demo
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")
Demo
What is your name? Anthony [Enter]
Nice to meet you, Anthony!

The Result is Always a String

No matter what the user types — even a number — input() always hands your program back a str.

The Result is Always a String

Demo
age = input("How old are you? ")
print(type(age))
Demo
How old are you? 16 [Enter]
<class 'str'>

Today's Objectives

  • Collecting input from the user
  • Storing input in a variable
  • Combining input with f-strings

Key Terms

Console Input
Text a user types into the terminal while a program is running.
input()
A function that displays a message and returns whatever the user types, as a string.
Prompt
The message shown to the user asking for input.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-7-input.


Task 1: Ask for Basic Info

  • Ask the user for their name and their favorite_animal.
  • Print an f-string that greets them using both answers.
Console Input
name = input("What is your name? ")
favorite_animal = input("What is your favorite animal? ")
print(f"Nice to meet you, {name}! {favorite_animal}s are pretty cool.")
#

Task 2: Ask a Few More Questions

  • Ask the user for their favorite_food and a hobby they enjoy.
  • Print an f-string that combines both new answers.
Console Input
name = input("What is your name? ")
favorite_animal = input("What is your favorite animal? ")
print(f"Nice to meet you, {name}! {favorite_animal}s are pretty cool.")
favorite_food = input("What is your favorite food? ")
hobby = input("What is a hobby you enjoy? ")
print(f"I heard you like {favorite_food} and enjoy {hobby} in your free time.")
#

Task 3: Build a Short Story

  • Ask the user for an adjective and a place.
  • Print a one-sentence story using both answers.
Console Input
name = input("What is your name? ")
favorite_animal = input("What is your favorite animal? ")
print(f"Nice to meet you, {name}! {favorite_animal}s are pretty cool.")
favorite_food = input("What is your favorite food? ")
hobby = input("What is a hobby you enjoy? ")
print(f"I heard you like {favorite_food} and enjoy {hobby} in your free time.")
adjective = input("Give me an adjective: ")
place = input("Give me a place: ")
print(f"One day, a {adjective} adventure began at the {place}.")
#

Challenge (Optional): One More Question

  • Ask one more question of your choice, and work the answer into your story sentence too.
divider

Checkpoint

Verify your program works correctly.

Example Output
What is your name? Anthony [Enter]
What is your favorite animal? Dog [Enter]
Nice to meet you, Anthony! Dogs are pretty cool.
What is your favorite food? pizza [Enter]
What is a hobby you enjoy? gaming [Enter]
I heard you like pizza and enjoy gaming in your free time.
Give me an adjective: mysterious [Enter]
Give me a place: library [Enter]
One day, a mysterious adventure began at the library.
divider

Reflection

Answer the following questions before submitting your work.

  1. What data type does input() always return, no matter what the user types?
  2. Why is it useful to write the question directly inside input() instead of printing it separately first?
  3. What was tricky, if anything, about building the short story in Task 3?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete