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.

It would help if you wrapped your code in [/ CODE] tags[CODE ] [/ CODE] tags

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

Sorry, I'm new to this sort of thing.

Dont worry, this can be lots easier. First we can find the minimun of a list by using the min function, the max by using the max function!

So :

li = [1,2,3,4,5,6,7,8,9,1,11,5,0,64]
print min(li) #0
print max(li) #64
print li.index(max(li))  #position 13
print li.index(min(li))   #position 12

To get the numbers you can use a loop like this:

li = []
value = '-1'
while value != '':
    li.append(int(raw_input("Enter a number please, just press enter to stop")))

Then for the average you just need another loop, this time a for loop:

sum = 0
for item in list:
    sum += item
print "average is:", (sum*1.0)/;en(li)

That should help you out, just try piece it together for what you need to do.

The problem is that you never call the function avg.

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

        ## this will print "function avg", etc because that is what avg is 
        print "average is", avg
        ## this will execute the function
        print "average is", avg(list_name)

        sys.exit(0)

as paulthom said, you want a while loop that will get the numbers and append each number to a list (you don't keep them now so you have nothing to average). Do nothing else at input. When all the numbers have been entered, pass the list to the avg() function. Make one pass through the list getting the smallest and largest and adding each number to a total variable (but don't use the name "sum" as that is a reserved word). Then divide as you have already and return the smallest, largest, and average.

I really appreciate you taking the time to help-I decided though to another by using the built-in functions of Python-this to me solved the issues I was having, but a couple new ones came up. Here is the code below:

def average(l1):
    length = len(l1)
    sum = 0
    for i in range(length):
        sum = sum + l1[i]
    average_value = sum / length
    return average_value



print 'Enter five numbers, pressing "Enter" after each:'
l1 = [int(raw_input()) for i in xrange(5)]
print l1
print "Smallest is", min(l1)
print "Largest is", max(l1)
print "The count of numbers is", len(l1)
print "Average is", average(l1)
print "The location of the largest number is", l1.index(max(l1))
print "The location of the smallest number is", l1.index(min(l1))
print len(l1 == 1)

The issue that I'm having is with giving the location of the max and min, semantically it feels more correct to give the first number in a list the location or position of 1 rather than 0, as such returning the largest number with its location being one less than it should be comes off as a little odd. Any thoughts on how to deal with the problem?

(Also ignore the very last line of code-that is work in progress on providing a count of the even numbers)

you could just do

print "The location of the largest number is", (1+l1.index(max(l1))

or something similar

semantically it feels more correct to give the first number in a list the location or position of 1 rather than 0

The first element of a list is the zero element because zero is the memory offset. To find an element, you go to the beginning of the block of memory and then go forward to the location of that element. So for the first element you go forward zero, for the second element you go forward one times the length, so if it is a 4 byte integer, then the second element would be at memory offset one times four, etc. You are perhaps coming from a word processor view point where there are lines of data in a document in a folder (where the first line is line #1). None of those things really exist. Programmers deal with streams/blocks of memory, but that is another topic. As programmers, we can either learn an existing language as someone else has written it, or write our own as we want it. Most of us are not capable of writing our own, so we learn and use what is already there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.