Input function | BUS 104 – Python Exam

Still feeling nervous for your exam? Check out the Python Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!

The input() function explained

What's your favorite color?

Someone asking another person what their favorite color is

In the real world, we verbally ask this question.

In Python, we'd utilize an input() function to ask the question.

The input() function enables us to ask the user for a value.

Variables explained

After asking someone for their favorite color, we'd remember their response mentally.

In Python, we need to remember this response with a variable.

Variables are utilized to remember values for later use in a program.

This is commonly used with input() functions to save the values that users input for later use in the program.

Coding an input() function

Let's write an input() function asking the user for their favorite color. Then, we'll utilize a variable to remember it and print it in the output.

Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)

Let's check out this template for input() functions.

input(question)

Since we're asking the user for their favorite color, we'll make our question ask this.

input("What is your favorite color?")

If we were to run this single line of code, the user's response would not be stored. We wouldn't be able to reference it later in the program.

Why? Because we haven't declared a variable to remember the user's input.

Declaring variables to remember

Let's declare a variable for the input() function that we have. We'll call it favorite_color...

favorite_color = input("What's your favorite color?")

Now, whatever the user inputs will be saved to the value of favorite_color.

You might be asking, why is it "favorite_color" and not "favorite color" or "Favorite Color"?

Because...

In Python, you must declare your variables without spaces and lowercased. Any spaces should be depicted with an underscore.

Although we're remembering the value of the user's favorite color from the input() into favorite_color, we're not doing anything meaningful with that variable yet.

Let's test that Python has actually remembered the value of favorite_color with a print() function!

favorite_color = input("What's your favorite color?")
print(favorite_color)

Essentially, what we're doing here is printing the value of whatever the user enters in the input() in the output.

Type the above code in your new Trinket file. It should look like so:

Now, press the "Run" button. You should get the following output, depicted in the slideshow below:

  • User is asked for their favorite color.

Notice how after we press enter in the input(), the favorite_color variable's value successfully appears in the output!

Inputting integers and floats

The input() function automatically returns values as strings.

Therefore, when dealing with input() functions that should return integers, we should use the following template:

int(input(question))

For floats, follow this template:

float(input(question))

Why do we place "int()" and "float()" around the input() function?

Because it forces the input() to be returned as an integer or float rather than a string.

Whenever your input() needs to return an integer instead of a string, surround it with int()!
Whenever your input() needs to return a float instead of a string, surround it with float()!

Not doing this properly can result in annoying errors later on in your program.

Practice problem

Create a new Trinket file that asks the user the question, "What's your name?" with an input() function. Save this value as a variable. Then, after they type their answer and press enter, print their name in the output.

Check out the slideshow below to see how your output should look.

  • User is asked for their name.

HINT: You need to print the user's name. How do we print? Check the print() function template on the last page of your Python Cheat Sheet for some help!

name = input("What's your name?")
print(name)

If you named your variable name anything else, that's okay. As long as you're output was the same, you're good to go!

Python Follow-along Guide

It's no secret you retain info better when you write it down. That's why I created the Python Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Python Follow-along Guide so that you remember all the important stuff for later!

You can obtain your Python Follow-along Guide for FREE by entering your email below!

Next up, we'll learn how to combine strings with variables to make dynamic messages with concatenation. Click below to continue!

Concepts

Each exam concept broken down with relatable situations to your life!

ToolsPython Follow-along Guide (FREE)
LessonPrint function
LessonInput function
LessonConcatenation
LessonConditionals (if, elif, else)
LessonWhile loops
LessonFor loops
LessonData Types
LessonNested Conditionals
LessonFlag Variables

Practice Problems

Include step-by-step explanations through each part of the problem!

Python Cram Kit Logo

"...I was blown away with the videos and the ability to explain what is going on at a simple level. Highly recommend!"

Jack Whelan
March 8, 2021

Rating: 5 out of 5.

Leave a Comment