Could someone lend me their brain? :D I thought I had most of it done, until I got an error saying that my 'calcAverage' definition was not defined.. x.x here is the prompt for the program:

Write a program that asks the user how many pumpkin weights they have, and then reads that many pumpkin weights, printing each weight with a comment (heavy is 70 pounds or higher, light is below 50, normal is everything inbetween), averages them, and displays the average with three decimal places. Use functions, one of which must be calcAverage(totalWeight, numPumpkins) to average the input values.

def main():
    
    intro()
    
    pumpkinData()
    
    calcAverage(totalWeight, numPumpkins)

    printResults(numPumpkins, avgWeight)

main()

def intro():
    print "Program to calculate the average of a"
    print "group of pumpkin weights."
    print "You will be asked to enter the number of"
    print "pumpkins, followed by each pumpkin weight."
    print "Written by Kristy Barner."
    print

def pumpkinData():
    numPumpkins = input("Enter the number of pumpkins: ")
    print
    print
    totalWeight = 0
    count = 0
    for i in range(numPumpkins):
        count = count + 1
        pumpkinWeight = input("Enter the weight for pumpkin "+str(count)+": ")
        totalWeight = total + pumpkinWeight
        if pumpkinWeight < 50.000:
            print str(pumpkinWeight)+" is light"
        else:
            if pumpkinWeight < 70.000:
                print str(pumpkinWeight)+" is normal"
            else:
                print str(pumpkinWeight)+" is heavy"
    return numPumpkins
    return totalWeight

def calcAverage(totalWeight, numPumpkins):
    avgWeight = float(totalWeight) / float(numPumpkins)
    return avgWeight

def printResults(numPumpkins, avgWeight):
    print "The average weight for "+str(numPumpkins)+" is "+str(avgWeight)

Thanks very much in advance!!

Member Avatar for masterofpuppets

hi :)

the problem is that you are calling the main function before you've defined the other ones, so a simple solution is to either call the main after you've defined all the other stuff or simply define main after all other functions.
The other thing about main is that when a function returns a value it is best to assign that value to a variable, i.e totalWeight, numPumpkins = pumpkinData() and then call calcAverage with totalWeight and numPumpkins, because otherwise they are not defined. And lastly I think you made a mistake in line 30, i.e. total is not defined. It should be totalWeight :), here's my version with the corrections:

def intro():
    print "Program to calculate the average of a"
    print "group of pumpkin weights."
    print "You will be asked to enter the number of"
    print "pumpkins, followed by each pumpkin weight."
    print "Written by Kristy Barner."
    print
 
def pumpkinData():
    numPumpkins = input( "Enter the number of pumpkins: " )
    print
    print
    totalWeight = 0
    count = 0
    for i in range(numPumpkins):
        count = count + 1
        pumpkinWeight = input("Enter the weight for pumpkin "+str(count)+": ")
        totalWeight = totalWeight + pumpkinWeight
        if pumpkinWeight < 50.000:
            print str(pumpkinWeight)+" is light"
        else:
            if pumpkinWeight < 70.000:
                print str(pumpkinWeight)+" is normal"
            else:
                print str(pumpkinWeight)+" is heavy"
    return totalWeight, numPumpkins
 
def calcAverage( totalWeight, numPumpkins ):
    avgWeight = float( totalWeight ) / float( numPumpkins )
    return avgWeight
 
def printResults( numPumpkins, avgWeight ):
    print "The average weight for %d is %.3f" % ( numPumpkins, avgWeight ) #three decimal spaces

def main():
    intro()
    totalWeight, numPumpkins = pumpkinData()
    avgWeight = calcAverage( totalWeight, numPumpkins )
    printResults( numPumpkins, avgWeight )
    
main()
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.