Python- Day 20- User Input
Many programs are designed to solve real-world problems for users. To do so, they need to receive input from the user.
- For example, a program designed to check if a user is old enough to vote requires the user to input their age.
Based on the input, the program can perform calculations or comparisons and provide feedback to the user.
The input()
Function:
The input()
function is used to prompt the user to enter some data.
It pauses the program, waits for the user’s input, and assigns the input to a variable.
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
>>
Tell me something, and I will repeat it back to you: Hello everyone!
Hello everyone!
The input()
function accepts one argument: a prompt that tells the user what type of information to enter.
Writing Clear Prompts:
- Clear, well-structured prompts are crucial for getting the correct input from the user.
name = input("Please enter your name: ")
print(f"\nHello, {name}!")
>>
Please enter your name: Nidhi
Hello, Nidhi!
- Adding spaces and separating the prompt from the input makes it easier for users to know where to enter their data.
- Longer prompts can be broken into multiple lines using string concatenation, which helps explain why certain input is needed.
prompt = "If you share your name, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print(f"\nHello, {name}!")
>>
If you share your name, we can personalize the messages you see.
What is your first name? Nidhi
Hello, Nidhi!
Converting Input to Numbers Using int()
:
By default, the input()
function interprets all user input as a string, even if the user enters a number.
age = input("How old are you? ")
How old are you? 21
>>>
age
'21'
If you need to perform calculations or comparisons (e.g., checking if the user’s age is greater than 18), you must convert the input to an integer using the int()
function.
age = input("How old are you? ")
age = int(age) # Convert the input string to an integer
if age >= 18:
print("You are old enough to vote.")
>>
How old are you? 18
You are old enough to vote.
Handling Numerical Input:
Numerical input can be used in a variety of ways, such as making comparisons or performing calculations.
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 48:
print("You're tall enough to ride!")
else:
print("You'll be able to ride when you're a little older.")
>>
How tall are you, in inches? 22
You'll be able to ride when you're a little older.
The Modulo Operator (%):
The modulo operator divides one number by another and returns the remainder.
This is useful for determining if a number is divisible by another (e.g., if a number is even or odd).
number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)
if number % 2 == 0:
print(f"The number {number} is even.")
else:
print(f"The number {number} is odd.")
>>
Enter a number, and I'll tell you if it's even or odd: 7458942342
The number 7458942342 is even
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!