Hello, I was assigned the task of coding this program in python for homework and I recently ran into a problem. Basically the assignment asks that a program prompt the user for a series of numbers and will then output the smallest number, the largest (their respective locations) and finally the average. The parts that have been tripping me up have been the "smallest number" output and outputting the average. Here's the code:
import sys
import types
smallest = 1000090
largest = 0
location = 0
loc_largest = 0
loc_smallest = 0
def avg(*L):
if len(L)==1 and (type(L[0]) is types.ListType or\
type(L[0]) is types.TupleType) :
L=L[0]
x=0.0
if len(L)==0: return x
for y in L: x+=y
return x/len(L)
while True:
location = location + 1
num = input("enter number ")
if num == "done": #check for end of input
print "the largest is ", largest
print "at location ", loc_largest
print "the smallest is ", smallest
print "at location ", loc_smallest
print "average is", avg
sys.exit(0)
else:
if num > largest: #found new largest
largest = num
loc_largest = location
if num < smallest: #found new smallest
smallest = num
loc_smallest = location
The issue with "smallest" is that it works with all values, but I feel that somethings not quite right with me simply assigning that arbitrary number. Even though that portion of the program works just fine, is there a more elegant solution or should I just be satisfied with what I have?
My other issue is with outputting the average, on this front I am not so clear, I thought "avg" had been defined but apparently I only return "average is <function avg at 0x02B964B0>" while I have tried multiple Solutions the end result is the same-the program does not output the average of the numbers that were input.
Thanks for helping me work on this problem.