vendredi 26 juin 2020

how can i find and write a list even and odd numbers?

A program that reads a sequence of numbers and counts how many numbers are even and how many are odd. When numbers of even output maybe 4, then the list is show even numbers and odd number. i got error in my code, this is my code:

odd_numbers = 0
even_numbers = 0
lst_odd =[]
lst_even =[] 
# read the first number
number = int(input("Enter a number or type 0 to stop: "))

# 0 terminates execution
while number != 0:
# check if the number is odd
   if number % 2 == 1:
        # increase the odd_numbers counter
        odd_numbers += 1
        return lst_odd
   else:
    # increase the even_numbers counter
    even_numbers += 1
    return lst_even
    # read the next number
   number = int(input("Enter a number or type 0 to stop: "))


 # print results
 print("Odd numbers count:", odd_numbers, '\n','list odd numbers is', lst_odd)
 print("Even numbers count:", even_numbers, '\n','list odd numbers is',lst_even)

i want to make the out like that:

Enter a number or type 0 to stop:  3
Enter a number or type 0 to stop:  3
Enter a number or type 0 to stop:  4
Enter a number or type 0 to stop:  5
Enter a number or type 0 to stop:  6
Enter a number or type 0 to stop:  7
Enter a number or type 0 to stop:  8
Enter a number or type 0 to stop:  2
Enter a number or type 0 to stop:  4
Enter a number or type 0 to stop:  0
Odd numbers count: 4
list odd number is [3,3,5,7] 
Even numbers count: 5
list even number is [4,6,8,2,4]

Aucun commentaire:

Enregistrer un commentaire