Given an integer as an input to a Python program your task is to check if the given number Falls between 3 and 9. This should be checked using the Python function. The Python function should return true if the number Falls between 3 and 9 and false if the number does not fall between 3 and 9
Can be done using Python special comparison. Keep reading below.
Write A Python Function To Check Whether A Number Falls Between 3 And 9
Let's define a function named check and pass the input argument to the function as a floating point number. using the special comparison that Python offers, compare the number using the if statement if it's between 3 and 9. if the number is between 3 and 9 Return the Boolean value as True. Else if the given number is not between the three and nine return the Boolean value as False from the function
def check(n):
if 3 <= n <= 9:
return True
return False
In the main program take a variable named as number and ask the user to enter a number and convert the number into a floating point. once we get the number as an input to the Python program. use the path to directly call the function check() To check if the number is between 3 and 9.
If the return value of the function is to print the number is between 3 and 9 using the else part print the number is not between 3 and 9 this completes the python program to check the number is between 3 and 9 using a python function.
number = float(input("Enter Number : "))
if check(number):
print("Number falls between 3 and 9")
else:
print("Number doesnt falls between 3 and 9")
Complete Python Working Code
====================================
def check(n):
if 3 <= n <= 9:
return True
return False
number = float(input("Enter Number : "))
if check(number):
print("Number falls between 3 and 9")
else:
print("Number doesnt falls between 3 and 9")
Output
================
Enter Number : 3.01
Number falls between 3 and 9
Output
==================
Enter Number : 9.000001
Number doesnt falls between 3 and 9
Output
=====================
Enter Number : 6
Number falls between 3 and 9
Output
====================
Enter Number : 0
Number doesnt falls between 3 and 9
Output
====================
Enter Number : 39
Number doesnt falls between 3 and 9
Conclusion
==================
Execute the above program by yourself and note down the results by providing the random input integer / floating point values to the Python program
Comment down below if you have any suggestions to improve the above program
0 Comments