TOPIC 7: Python User input

Site: Learning Management System
Course: Edo College ICT Club Programme
Book: TOPIC 7: Python User input
Printed by: Guest user
Date: Monday, 23 November 2026, 1:24 AM

1. Introduction

1. User Input

Python allows for user input. That means we are able to ask the user for input.

The following example asks for your name, and when you enter a name, it gets printed on the screen.

Example: Ask for user input

print("Enter your name:")
name = input()
print(f"Hello {name}")

Sample run:

Enter your name:
Ada
Hello Ada

Python stops executing when it comes to the input() function, and continues when the user has given some input.

2. Using a Prompt

In the example above, the user had to input their name on a new line. The Python input() function has a prompt parameter, which acts as a message you can put in front of the user input, on the same line.

Example: Add a message in front of the user input

name = input("Enter your name:")
print(f"Hello {name}")

Sample run:

Enter your name:Ada
Hello Ada

3. Multiple Inputs

You can add as many inputs as you want. Python will stop executing at each of them, waiting for user input.

Example: Multiple inputs

name = input("Enter your name:")
print(f"Hello {name}")
fav1 = input("What is your favorite animal:")
fav2 = input("What is your favorite color:")
fav3 = input("What is your favorite number:")
print(f"Do you want a {fav2} {fav1} with {fav3} legs?")

Sample run:

Enter your name:Ada
Hello Ada
What is your favorite animal:dog
What is your favorite color:blue
What is your favorite number:4
Do you want a blue dog with 4 legs?

4. Input Numbers

The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string.

You can convert the input into a number with the float() function.

Example: To find the square root, the input has to be converted into a number

import math

x = input("Enter a number:")

# find the square root of the number:
y = math.sqrt(float(x))

print(f"The square root of {x} is {y}")

Sample run:

Enter a number:16
The square root of 16 is 4.0

Note: The line import math at the top is needed because the program uses math.sqrt(). Without it, Python will show an error.

5. Validate Input

It is good practice to validate any input from the user. In the example above, an error will occur if the user inputs something other than a number.

To avoid getting an error, we can test the input. If it is not a number, the user gets a message like "Wrong input, please try again" and is allowed to make a new input.

Example: Keep asking until you get a number

y = True
while y == True:
    x = input("Enter a number:")
    try:
        x = float(x)
        y = False
    except:
        print("Wrong input, please try again.")

print("Thank you!")

Sample run:

Enter a number:abc
Wrong input, please try again.
Enter a number:7
Thank you!

6. Summary

  • The input() function asks the user to type something, and Python waits until they do.
  • Use the prompt parameter, for example input("Enter your name:"), to show a message on the same line as the input.
  • You can use as many input() calls as you need.
  • Input is always treated as a string. Use float() to convert it into a number.
  • Validate input with try and except inside a while loop so the program does not crash on wrong input.