Python odd or even program


Asked by maham237 @ in Computers And Technology viewed by 374 People


Python
Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. If the input is: 5 2 4 6 8 10 Then the output is: all even If the input is: 5 1 3 5 7 9 Then the output is all odd If the input is: 5 1 2 3 4 5 Then the output is: not even or odd Your program must define and call the following three functions. def GetUserValues(). GetUserValues reads in, creates, and returns the list of integers. def IsListEven(myList). IsListEven returns true if all integers in the list are even and false otherwise. def IsListOdd(myList). IsListOdd returns true if all integers in the list are odd and false otherwise.

Answered by maham237 @



Answer:

Following are the function to the Python Programming Language.

#define a function to read or crates the list

def GetUserValues():

my_list=[]

n=int(input("Enter range: "))

for i in range(n):

my_list.append(int(input(" :")))

return my_list

#define a function to check the elements of the list

def IsListEven(mylist):

#set the for loop

for i in range(len(mylist)):

#set if condition to check odd number

if mylist[i]%2 !=0:

#if condition is true retun false

return False

#otherwise return true

return True

#define a function to check the elements of the list

def IsListOdd(mylist):

#set the for loop

for i in range(len(mylist)):

#set if condition to check even number

if mylist[i]%2==0:

#if condition is true retun false

return False

#otherwise return true

return True


#set variable to store list

l=GetUserValues()

#set if condition to check the function is true

if IsListOdd(l)==True:

#then, print the message

print("all odd")

#set elif condition to check the function is true

elif IsListEven(l)==True:

#then, print the message

print("all Even")

else:

#otherwise print message.

print("not even or odd")

Output:

Enter range: 5

:2

:4

:6

:8

:10

all Even

Explanation:

Here, we define a function "GetUserValues()" and inside the function.

  • Set the list data type variable "my_list" then, get input from the user in the variable "n".
  • Set the for loop which starts from 0 and end at the variable "n" then, get input from the user and appends inputs in the variable "my_list".
  • Then, we return list and close the function.

Then, we set a function "IsListEven()" and pass an argument "mylist" to check the elements is even or not, if the list elements is even then, return true otherwise, return false.

Then, we set a function "IsListOdd()" and pass an argument "mylist" to check the elements is odd or not, if the list elements is odd then, return true otherwise, return false.

Finally, we call and store the function in the variable "l" then, set conditions to check the list is even print "all even", if the is odd "all odd" otherwise, print "not even or odd".


Similar Questions

Even and odd numbers python

Asked by maham237 @ in Computers And Technology viewed by 497 persons

Python Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating …

How to tell if a polynomial is even or odd

Asked by admin @ in Mathematics viewed by 319 persons

Describe the end behavior of polynomial graphs with odd and even degrees. Talk about positive and negative leading coefficients.

How to program a tic tac toe board in python

Asked by admin @ in Computers and Technology viewed by 333 persons

Write a program for TIC TAC TOE in PYTHON

Program to find greatest of three numbers in python

Asked by admin @ in Computer Science viewed by 346 persons

Write a program in python to find the greatest number among three numbers​

Write a program to calculate compound interest in python

Asked by admin @ in Physics viewed by 423 persons

Write a program in python to calculate the compound interest​

Write a program to display your name in python

Asked by admin @ in Computer Science viewed by 350 persons

Write a code in python to display your name and age.

Write a program to swap two numbers in python

Asked by admin @ in Computer Science viewed by 354 persons

Write a program is python to swap two numbers.

Bind function in python socket programming is used to

Asked by admin @ in Computer Science viewed by 385 persons

Bind() function in Python socket programming is used to: 1. Specify the port for service on the specified host2. Read data from the socket 3.Send data to the socket …

Python program to find all roots of a quadratic equation

Asked by admin @ in Computer Science viewed by 366 persons

Write a python program to find all roots of a quadratic equation​

Which two languages contributed to python as a programming language

Asked by admin @ in Computer Science viewed by 357 persons

Which two languages contributed to Python as a programming language

Program to circulate the values of n variables in python

Asked by admin @ in Computer Science viewed by 4056 persons

Ii)Write a python program to circulate the values of n variables.​

Python program to print first n numbers divisible by 5

Asked by admin @ in Computer Science viewed by 359 persons

Write a Python program using while loop first N numbers divisible by 5.

Bank account python program

Asked by maham237 @ in Computers And Technology viewed by 341 persons

Write a Python program to balance a checkbook. The main function needs to get the initial balance, the amounts of deposits, and the amounts of checks. Allow the user to …

Is 56 even or odd

Asked by maham237 @ in Mathematics viewed by 307 persons

Find the sum of two consecutive odd numbers is 56 find the numbers

How to determine if a graph is even or odd

Asked by admin @ in Mathematics viewed by 258 persons

1. How can you determine from a graph if a function is even, odd, or neither? 2. How can you determine from an equation if a function is even, odd …

Most viewed questions in Computers And Technology


Codehs python answer key

Asked by maham237 @ in Computers And Technology viewed by 2086 persons


3.4 6 t shirt shop codehs answers

Asked by maham237 @ in Computers And Technology viewed by 1861 persons



Security infrastructure design document

Asked by wiki @ in Computers And Technology viewed by 1809 persons


Codehs python answers

Asked by maham237 @ in Computers And Technology viewed by 1173 persons


In the business world people are often measured by their

Asked by maham237 @ in Computers And Technology viewed by 1105 persons




Banana fee fi fo fana song

Asked by maham237 @ in Computers And Technology viewed by 800 persons


List 10 www standards that the w3c develops and maintains

Asked by maham237 @ in Computers And Technology viewed by 787 persons


Open the excel workbook revenue xls from the default directory

Asked by maham237 @ in Computers And Technology viewed by 753 persons



In order to protect your computer from the newest viruses

Asked by maham237 @ in Computers And Technology viewed by 746 persons