Q: 1. Write a program that read a sequence of positive integer inputs and print
ID: 3789732 • 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 not as a print screen.
Explanation / Answer
Please run them in python 2.7
Input space to come out of while loop
Q1:
def main():
summ=0
count=0
while(1):
num=raw_input("Enter an int value or empty space to end: ")
if (num==" "):
break
else:
num=int(num)
summ+=num
count+=1
print("Sum is: ",summ)
print("Average is: ",summ/count)
main()
Q2:
def main():
odd=0
while(1):
num=raw_input("Enter an int value or empty space to end: ")
if (num==" "):
break
else:
num=int(num)
if(num%2!=0):
odd+=1
print("No. of odd inputs: ",odd)
main()
Q3:
def main():
summ=0
count=0
while(1):
num=raw_input("Enter an int value or empty space to end: ")
if (num==" "):
break
else:
num=int(num)
if(num%2!=0):
count+=1
summ+=num
print("Average: ",summ/count)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.