Develop a Python program which inputs a series of integers and processes them. T
ID: 3842597 • Letter: D
Question
Develop a Python program which inputs a series of integers and processes them. The program will: a) Continue to process values until the user enters the value 0 b) Ignore all negative integers c) Count the number of odd integers entered d) Count the number of even integers entered e) Calculate the sum of the odd integers in the series f) Calculate the sum of the even integers in the series g) Display the sum of odds h) Display the sum of evens i) Display the count of odds j) Display the count of evens k) Display the total number of positive integers entered i) BONUS: print a message whenever a negative integer is entered Sample output: Input an integer (theta terminates): 1 Input an integer (theta terminates): 3 Input an integer (theta terminates): -2 Input an integer (theta terminates): 2 Input an integer (theta terminates): 6 Input an integer (theta terminates): 5 Input an integer (theta terminates): theta sum of odds: 9 sum of evens: 8 odd count: 3Explanation / Answer
Python source code : The below is working fine i have also included the error message for negative numbers when it is encountered while taking the user input.
import sys
l = []
e = []
o = []
while(1):
choice = int(input('Input an integer(0 terminates): '))
if choice<0:
print 'Negative number entered'
continue
if choice>0:
l.append(choice)
tup = tuple(l)
if choice % 2 == 0:
e.append(choice)
else:
o.append(choice)
continue
if choice==0:
print 'The positive integer count is',len(tup),' ','The count of even numbers is',len(e),' ','The sum of evens is',sum(e),' ','The count of odd numbers is',len(o),' ','The sum of odds is',sum(o)
sys.exit(0)
output :
Input an integer(0 terminates): 1
Input an integer(0 terminates): 2
Input an integer(0 terminates): -2
Negative number entered
Input an integer(0 terminates): 3
Input an integer(0 terminates): 0
The positive integer count is 3
The count of even numbers is 1
The sum of evens is 2
The count of odd numbers is 2
The sum of odds is 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.