363,077 views
35 votes
35 votes
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.

User LostAtSeaJoshua
by
3.2k points

1 Answer

17 votes
17 votes

def is_list_even(my_list):

for i in my_list:

if(i%2 != 0):

return False

return True

def is_list_odd(my_list):

for i in my_list:

if(i%2 == 0):

return False

return True

def main():

n = int(input())

lst = []

for i in range(n):

lst.append(int(input()))

if(is_list_even(lst)):

print('all even')

elif(is_list_odd(lst)):

print('all odd')

else:

print('not even or odd')

if __name__ == '__main__':

main()

User Stefan Wallin
by
2.9k points