Ask Question
6 October, 03:29

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.

+4
Answers (1)
  1. 6 October, 03:55
    0
    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".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Python Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers