Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Basics part 1
Syntax
Short description
break
break interrupts code that is repeated
continue
continue interrupts a repetition and starts the next
elif
elif is a combination of else and if
else
else can be used last in an if-statement
float()
Converts to type float (decimal number)
if
if controls the code to do different things depending on a condition
input()
Receives input from the user
int()
Converts to type int (integer)
randint()
Generates a random integer
round()
Rounds a number
str()
Converts to type string
while
while is used to repeat code

User Input

In this section you will learn to

The input function

Most programs need some kind of input from the user. Often the user communicates with the program through the mouse or keyboard, but it can also be a file that is input to the program. We will look at how we can enter information into the program using the keyboard.

With the input function, Python reads a line of text from the user. Here at Pythonlab.dev, a pop-up window will appear where you can type when you use the input function. The result of input is a string, we say that the input function returns a string.

Example

Reads a line of text and then prints what you have written.

text = input()
print(text)

Example

You can write a message to the user in the argument to input.

name = input('What is your name?')
print('Hello ' + name + '!')

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program prints. Run the program after you answer and see if you were correct.

-- Output from your program will be here --

Question: What will the program print?

<class 'float'>
<class 'int'>
<class 'str'>

The input function thus gives us a string. We say that the function returns a string. This means that we must convert the return value if we ask the user for a number and then want to calculate with it. The risk is that the user types something that cannot be converted to a number, and then our program will not work. This can be solved with error handling, but we will not learn that now.

Example

Calculate the area of a square. Feel free to try a decimal number, remember to use a dot. What happens if you enter something that cannot be converted to a number?

side = input('Enter the side of the square:')
#the string side must now be converted to a decimal number
side = float(side)
area = side * side

Login or create an account to save your progress and your code.

Find the issue

The code does not work as it should. Can you fix it? Remember that decimal numbers should work as input.

-- Output from your program will be here --

Login or create an account to save your progress and your code.

Calculate the area of a triangle

The program should take two inputs from the user: the height and the base of a triangle. Print the area of the triangle (just print the number). The program should be able to handle decimal numbers.

-- Output from your program will be here --

Do you want to practice a similar task? See the activity Rectangle perimeter.

Status
You have finished all tasks in this section!
🎉