I have to write a program that asks the user how many pumpkin weights they have, and the reads that many pumpkin weights, printing each weight with two decimeal points and a comment and displays data about them to four decimal places. I need to find the average weight, maximum weight and minimum weight as well. I have to use functions as well. I thought I finished my program but I have continuously been getting an error saying that a str object cannot be interpreted as an integer. It may not even be that. I may have something else in my program wrong that I havent been able to catch. If you could help that would be great thank you!

# Purpose: write a program that asks the user
#          how many pumpkin weights they have,
#          and then reads that many pumpkin
#          weights, printing each weight with
#          two decimal points and a comment, 
#          and displays data about them to four
#          decimal places using functions

def intro():

# print out the introduction

    print()
    print("Program to calculate statistics 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 ARH.")
    print()

def pumpkinInputs():
    numPumpkins = input("Enter the number of pumpkins: ")
    print()
    print()
    totalWeight = 0
    count = 0
    for pumps in range(numPumpkins):
        count = count + 1
        pumpWeight = int(input("Enter the weight for pumpkin "+str(count)+": "))
        totalWeight = totalWeight + pumpWeight
        if (pumpWeight >= 1500.00):
            print(str(pumpWeight) + " is humungous")
        elif (pumpWeight > 500.00):
            print(str(pumpWeight) + " is huge")
        elif (pumpWeight >= 300.00):
            print(str(pumpWeight) + " is heavy")
        elif (pumpWeight >= 100.00): 
            print(str(pumpWeight) + " is medium")
        elif (pumpWeight >= 50.00):
            print(str(pumpWeight) + " is featherweight")
        else:
            print(str(pumpWeight) + " is light")
    return totalWeight, numPumpkins

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

    pumpkins = (numPumpkins + 1)
    maxi = 0
    for num in pumpkins:
        weight = num
        if (weight > maxi):
            maxi = weight

    mini = maxi
    for num in pumpkins:
        weight = num
        if (weight < mini):
            mini = weight
    return maxi, mini


def printResults(numPumpkins, averageWeight):
    print("Number of pumpkins:", numPumpkins)
    print("The average weight is {0:0.4f}".format(averageWeight))
    print("The maximum weight is {0:0.4f}".format(maxi))
    print("The minimum weight is   {0:0.4f}".format(mini))
    print()        

def main():
    intro()
    totalWeight, numPumpkins = pumpkinInputs()
    averageWeight = calcAverage(totalWeight, numPumpkins)
    printResults(numPumpkins, averageWeight)
main()

Recommended Answers

All 5 Replies

but I have continuously been getting an error saying that a str object cannot be interpreted as an integer

We have no idea what or where this may be. Post the complete error message which includes the offending line and also state which version of Python you are using as one version will give an error but another version will not for the same line of code.

In the future, when getting this kind of error, please post the traceback and error message as well as he code where it is occurring. I was able to work out the problem this time, but the traceback makes it much easier to figure out the problem once you learn how to read it.

Also, always mention the version of Python you are using; Python 3.x, which is what you seem to be working in, is very different from the older Python 2.x regarding several things, including basic console I/O.

The specific error you are getting is on line 22:

    numPumpkins = input("Enter the number of pumpkins: ")

You are reading in the user input as a string, but - unlike in the code below it on line 29 - you don't convert it to an integer. You need to write it as:

    numPumpkins = int(input("Enter the number of pumpkins: "))

Note that even this isn't foolproof; if the user makes a typing mistake or deliberately enters a value other than an integer, an exception will get raised. To do this properly, you really should wrap the line in a while: loop, and then in a try:...except: block.

    numPumpkins = 0
    while numPumpkins == 0:
        # get the input value separately so you can include it in any error msgs you print out
        numString = input("Enter the number of pumpkins: ") 
        try:
            numPumpkins = int(numString)
        except ValueError:
            print("{0} is not an integer. Please try again.".format(numString))
            numPumpkins = 0

You should do the same for line 29 as well. This may seem like a lot of work just to read a number in, but it does ensure that you get a valid answer from the user.

I am trying to find the max and min of a set of numbers that the user inputs and this is the error i keep getting using Python3.2

Traceback (most recent call last):
  File "C:\Python32\Projects\Project9Pumpkins", line 80, in <module>
    main()
  File "C:\Python32\Projects\Project9Pumpkins", line 79, in main
    printResults(numPumpkins, averageWeight)
  File "C:\Python32\Projects\Project9Pumpkins", line 70, in printResults
    print("The maximum weight is {0:0.4f}".format(maxi))
NameError: global name 'maxi' is not defined






def pumpkinInputs():
    numPumpkins = int(input("Enter the number of pumpkins: "))
    print()
    print()
    totalWeight = 0
    count = 0
    for pumps in range(numPumpkins):
        count = count + 1
        pumpWeight = eval(input("Enter the weight for pumpkin "+str(count)+": "))
        totalWeight = totalWeight + pumpWeight
        if (pumpWeight >= 1500.00):
            print("{0:0.2f}".format(pumpWeight) + " is humungous")
        elif (pumpWeight > 500.00):
            print("{0:0.2f}".format(pumpWeight) + " is huge")
        elif (pumpWeight >= 300.00):
            print("{0:0.2f}".format(pumpWeight) + " is heavy")
        elif (pumpWeight >= 100.00): 
            print("{0:0.2f}".format(pumpWeight) + " is medium")
        elif (pumpWeight >= 50.00):
            print("{0:0.2f}".format(pumpWeight) + " is featherweight")
        else:
            print("{0:0.2f}".format(pumpWeight) + " is light")
    return totalWeight, numPumpkins

def calcAverage(totalWeight, numPumpkins):
    averageWeight = (totalWeight) / (numPumpkins)
    return averageWeight

    pumpkins = (numPumpkins + 1)
    maxi = 0
    for num in pumpkins:
        pumpWeight = num
        if (pumpWeight > maxi):
            maxi = weight

    mini = maxi
    for num in pumpkins:
        pumpWeight = num
        if (pumpWeight < mini):
            mini = weight
    return maxi, mini


def printResults(numPumpkins, averageWeight):
    print("Number of pumpkins:", numPumpkins)
    print("The average weight is {0:0.4f}".format(averageWeight))
    print("The maximum weight is {0:0.4f}".format(maxi))
    print("The minimum weight is   {0:0.4f}".format(mini))
    print()
    print()

def main():
    intro()
    totalWeight, numPumpkins = pumpkinInputs()
    averageWeight = calcAverage(totalWeight, numPumpkins)
    printResults(numPumpkins, averageWeight)
main()

You are refering to undefined variable.

commented: I'm not sure how to make the undefined variable defined though +0

When defining printResults you have not included maxi and mini.

commented: I tried including maxi and mini in my printResults but it still doesnt not work...i think i need to get my maxi and mini ifs in the for loops but im not sure which route to take +0
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.