Q: 1. Write a program that read a sequence of positive integer inputs and print
ID: 3792794 • Letter: Q
Question
Q: 1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]:
Enter an int value or empty string to end: 98
Enter an int value or empty string to end: 78
Enter an int value or empty string to end: 77
Enter an int value or empty string to end: 1
Enter an int value or empty string to end: 10
Enter an int value or empty string to end:
Sum: 264 Average: 52.8
2. Write a program that read a sequence of positive integers and print the number of odd inputs entered by the user. The sentinel value is an empty string. Use the following test case [48 points]:
Enter integer or empty string to end: 3
Enter integer or empty string to end: 2
Enter integer or empty string to end: 10
Enter integer or empty string to end: 5
Enter integer or empty string to end: 6
Enter integer or empty string to end: 7
Enter integer or empty string to end:
number of odd integers entered: 3
3.Write a program that read a sequence of positive integers and print the average value of all the odd integers entered by the user (hint: you need to compute the sum of all the odd integers first to calculate the average). Use the following test case [7 points]:
Enter an int value or empty string to end: 10
Enter an int value or empty string to end: 20
Enter an int value or empty string to end: 5
Enter an int value or empty string to end: 10
Enter an int value or empty string to end: 15
Enter an int value or empty string to end:
average : 10.0
use python code for all questions and give the outpu in word document and make in python cell file
Explanation / Answer
PROGRAM FOR QUESTION 1
#declaring list and variable
num_list=[]
s=0
#getting input from the user until empty string is typed(enter key)
while True:
n=input('Enter an int value or empty string to end: ')
#break from the loop when empty string is found
if not n:
break
#adding elements into the list
else:
num_list.append(int(n))
# when list is not empty if block is executed
if len(num_list)!=0:
#finding the sum of all elements in the list
s=sum(num_list)
#finding the avg of the elements in the list
avg=s/len(num_list)
#printing the result
print('Sum:%d Average:%.1f' % (s,avg))
# when no element is entered else block is executed
else:
print('NO element to add')
SAMPLE OUTPUT FOR QUESTION 1
Enter an int value or empty string to end: 23
Enter an int value or empty string to end: 43
Enter an int value or empty string to end: 44
Enter an int value or empty string to end: 76
Enter an int value or empty string to end: 32
Enter an int value or empty string to end:
Sum:218 Average:43.6
PROGRAM FOR QUESTION 2
#declaring list
num_list=[]
#getting input from the user until empty string is typed(enter key)
while True:
n=input('Enter an int value or empty string to end: ')
#break from the loop when empty string is found
if not n:
break
#adding elements into the list
else:
num_list.append(int(n))
# when list is not empty if block is executed
if len(num_list)!=0:
# declaring variable c to count the number of odd in the list
c=0
for i in range(len(num_list)):
if num_list[i]%2 !=0:
c+=1
print('number of odd intergers entered %s' % c)
# else block executed when no element is entered
else:
print('NO element to check')
SAMPLE OUTPUT
Enter an int value or empty string to end: 2
Enter an int value or empty string to end: 4
Enter an int value or empty string to end: 3
Enter an int value or empty string to end: 6
Enter an int value or empty string to end: 7
Enter an int value or empty string to end:
number of odd intergers entered 2
PROGRAM FOR QUESTION 3
#declaring list and variable
num_list=[]
s=0
#getting input from the user until empty string is typed(enter key)
while True:
n=input('Enter an int value or empty string to end: ')
#break from the loop when empty string is found
if not n:
break
#adding elements into the list
else:
num_list.append(int(n))
# when list is not empty if block is executed
if len(num_list)!=0:
#declaring variable to count the number of odd number in the list
c=0
for i in range(len(num_list)):
if num_list[i]%2 !=0:
#adding the odd number
s=s+num_list[i]
c+=1
avg=s/c
print('average %.1f' % avg)
#elese block executer when no element is entered
else:
print('NO element to add')
SAMPLE OUTPUT
Enter an int value or empty string to end: 12
Enter an int value or empty string to end: 13
Enter an int value or empty string to end: 14
Enter an int value or empty string to end: 15
Enter an int value or empty string to end:
average 14.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.