1. Write a complete program with comments and sample run Ask the user for a numb
ID: 3559416 • Letter: 1
Question
1.
Write a complete program with comments and sample run
Ask the user for a number . Your program will read and compute the average for that many (numbers) integers, display the result. Your average has to be formatted to one decimal point. You have to use a for loop and have only one variable for reading the integers.
Example: Please enter how many numbers you have
8
Please enter 8 numbers to calculate the average
1 2 3 4 5 6 7 8
Your average is 18.0
2.
You will write a complete program to simulate multiplication through addition. Your program will ask for two numbers (they may have decimal points) separated by *. Then your program will use loop (You have to use loop) to create a product by adding the first number . Then it will repeat the program if the user wants to continue. Your first number may be integer, decimal, or negative number. Your second number will only be positive integer- You have to check the second number to be sure it not negative but you do not need to check to see if it has decimal - We will assume it it integer
Ex:
User Input
9*3 (it has to be in this format)
Your program will do 9+9+9
Then displays 27
Then it asks the user if s/he wants to continue
Explanation / Answer
Codes written in Python. ( Language not specified, hence )
Code 1:
from __future__ import division
import sys
num = int(raw_input("Please enter how many numbers you have "))
inp = raw_input("Enter the numbers ")
inpa = inp.split()
if len(inpa) != int(num):
print "Please enter the correct number of inputs"
sys.exit()
sum = 0
for i in inpa:
sum = sum + float(i)
print "The average is %.1f" % (sum/num)
Code 2)
from __future__ import division
import sys
def mulfunc():
inp = raw_input("Please enter the input")
inpa = inp.split("*")
if len(inpa) != 2: # check if the input format is right
print("Input not sane")
sys.exit()
mtimes = inpa[1]
mply = inpa[0]
smul = 0
for i in range(int(mtimes)):
smul = smul + float(mply)
print "The answer is %.1f" % smul
ans = raw_input("Do you want to continue? y/n")
if ans == "y":
mulfunc()
else:
print "Exiting..."
sys.exit()
mulfunc()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.