Homework Help: Questions and Answers: Write a program that takes a number as input and checks if the number lies between 1 and 100 (inclusive) using if-else and logical operators in python.
Answer:
Let’s write a Python program that takes a number as input and checks if it lies between 1 and 100 (inclusive) using if-else
statements and logical operators.
Step 1: Take Input from the User
First, we need to take a number as input from the user. We can use the input()
function to do that and convert the input to an integer using int()
.
number = int(input("Enter a number: "))
Step 2: Use If-Else to Check the Condition
We need to check if the number lies between 1 and 100 (inclusive). For that, we will use the logical operator and
to check if the number is greater than or equal to 1 and less than or equal to 100.
if number >= 1 and number <= 100:
print("The number lies between 1 and 100.")
else:
print("The number does not lie between 1 and 100.")
Complete Code
Putting it all together, the complete Python program looks like this:
number = int(input("Enter a number: "))
if number >= 1 and number <= 100:
print("The number lies between 1 and 100.")
else:
print("The number does not lie between 1 and 100.")
Execution
If the user inputs 50
:
Enter a number: 50
The number lies between 1 and 100.
If the user inputs 150
:
Enter a number: 150
The number does not lie between 1 and 100.
Learn More: Homework Help
Q. Which of the following is a good practice for protecting your identity online?
Q. How can computers contribute toward creating a “paperless society”?