How to find the odd and even numbers in python ?
Lakshyaraj Dash
Posted on July 10, 2022
This is a simple program in python to detect the odd and even numbers from a list generated when the user gives his/her numbers automatically.
Basically here the logic is if (num %2 == 0)
then it's even or else odd.
def myFunction():
list1 = []
while True:
userInp = input("Enter your number or press q to exit\n")
if userInp == "q":
evens = []
odds = []
for i in range(0, len(list1)):
if int(list1[i]) % 2 == 0:
evens.append(list1[i])
else:
odds.append(list1[i])
print("Your list of numbers: ", list1)
if len(evens) == 0:
print("No even numbers detected.")
else:
print("Even numbers are:", evens)
if len(odds) == 0:
print("No odd numbers detected.")
else:
print("Odd numbers are:", odds)
break
else:
try:
userInp = int(userInp)
if userInp not in list(list1):
list1.append(userInp)
except ValueError:
print("Error: You have entered a non integer or a decimal fraction number.")
if __name__ == "__main__":
myFunction()
💖 💪 🙅 🚩
Lakshyaraj Dash
Posted on July 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
undefined Hai Robotics Achieved High Availability & Easy Operations in a Hybrid Cloud Architecture with JuiceFS
November 29, 2024