DEV Community

Sarah Bartley-Dye
Sarah Bartley-Dye

Posted on

Introduction to Python Module Two Part Three: Inputs and Outputs

The goal of any web app is to build interaction between the developers and their users. This is why inputs and outputs are important. This week, SoloLearn's Introduction to Python courses continue with module two by looking at inputs and outputs.

You will be able to explain what inputs and outputs are. This is also where you will begin using inputs and outputs in your code.

Inputs

Inputs are the information users give to the computer. It is how computers talk to users. Some real-life examples are pressing a key on a keyboard, pressing a button, or a video captured from a camera connected to a computer.

At Coding with Kids, students use inputs in their games to ask questions to start a game or program. For example, Level M students build a version of the hangman game called Snowman. In this game, students use inputs to ask the player for letters.

When player enters a letter, the computer checks to see if that letter is in the mystery word the computer has chosen. They can use the built in function input() to get these letters.

Python has built-in functions that developers can use to do a variety of things. The input() function lets the computer know it needs to get information from the user, so it will need to ask a question. Inside the parentheses, developers can put the question they want the computer to ask the user.

The input() function is often use with variables because developers want the user's response stored to be used again later in code. To create an input in Python, you will see a variable assigned to the input() function. Inside the input() function is a string asking the question a user answers.

name = input(“What is your name?”) # asks the user for their name
print(name) # Will print the name on screen
Enter fullscreen mode Exit fullscreen mode

In this code sample, the name variable asks the user a question as a string inside the input() function. When the user gives their answer, it will be stored as the value for the name variable. On the next line, the print() function will print the value of the name variable.

Outputs

The name value in the example above is the output. An output is how the computer talks back to the user. This is the response the computer has to the information the user gives.

Some real-life examples of outputs are messages displayed on the screen or sounds from a speaker. When developers get an error message in their code, that is an example of output from the computer. If you go to your doctor, they often print a medical summary after the visit, as well as the bill for what needs to be paid.

The print() function is often used to create outputs. You have been using the print() function a lot so far to print different data types so you can use the print() function to practice outputs as much as you like. As long as you put what you want the computer to do inside the parentheses of the print() function, the computer will output this data to the console.

print(“Hello World”) # will print Hello World
print(8) #will print 8
print(3.14) # prints 3.14

score = 10
print(score) # will print 10
Enter fullscreen mode Exit fullscreen mode

The above code shows outputs of different print statements. The code prints different data types and data stored inside a variable.

Top comments (0)