So I have to write a program that takes the amount and weight of pumpkins entered by a person and then outputs them back with their corrisponding weight class that i specify. I keep getting a syntax error on the elif statement and I was wondering if i could have some guidance as to what I'm doing wrong. The code is listed below, Thank you in advance !

def intro():

    print("Program to calculate the average of a")
    print("group of pumpkin weights.")
    print("You will be asked to entr the number of")
    print("pumpkins, followed by each pumpkin weight.")
    print("Written by Gregory Navestad.")
    print()

def pumpkinAmount():

    pumpkinNumber = int(input("Enter the number of pumpkins: "))
    print()
    print()
    totalWeight = 0
    count = 0
    for pumpkins in range(pumpkinNumber):
        count = count +1
        pumpkinWeight = int(input("Enter the weight for pumpkin " +str(count)+": "))
        totalWeight = totalWeight + pumpkinWeight
        if (pumpkinWeight >= 70.00):
            print(str(pumpkinWeight + " is heavy")
        elif (pumpkinWeight >= 50.00 and pumpkinWeight <= 70):
            print(str(pumpkinWeight + " is normal")
        else: (pumpkinWeight <= 50.00):
            print(str(pumpkinWeight + " is light")
    return totalWeight,pumpkinNumber

def calcAverage(totalWeight,pumpkinNumber):

    averageWeight = float(totalWeight) / float(pumpkinNumber)
    return averageWeight

def results(averageWeight,pumpkinNumber):
    print("The Average weight of",pumpkinNumber,"is {0:0.3f}".format(averageWeight))
    print()

def  main():
        intro()
        totalWeight, pumpkinNumber = pumpkinAmount()
        averageWeight = calcAverage(totalWeight,PumpkinNumbers)
        results()
main()

Dani AI

Generated

A short expert note: has already identified the immediate SyntaxError causes — the three print lines are missing a closing parenthesis and the final else was written with a condition (an else must not have one). Fixing those two things will stop the parser error. Several additional issues will still prevent correct behavior; the list below clarifies them and gives small, safe examples to apply.

Checklist and concrete fixes:

  • Balanced prints and proper string assembly. Convert the number to text or use formatting instead of putting the concatenation inside str(...):

    print(str(pumpkinWeight) + " is heavy")
    print("{} is normal".format(pumpkinWeight))
    # or (Python 3.6+)
    print(f"{pumpkinWeight} is light")
  • Use numeric type appropriate for weights. If fractional weights are possible, parse with float() rather than int():

    pumpkinWeight = float(input("Enter the weight for pumpkin {}: ".format(count)))
  • Simplify the condition logic so ranges do not require redundant checks. For example:

    if pumpkinWeight >= 70:
        ...
    elif pumpkinWeight >= 50:
        ...
    else:
        ...
  • Fix name and call mismatches that will raise NameError or wrong results: call calcAverage(totalWeight, pumpkinNumber) (not PumpkinNumbers) and call results(averageWeight, pumpkinNumber) with the arguments the function expects.

Quick debugging tips: run the script and read the SyntaxError line number first (it usually points to the missing parenthesis), then re-run to catch NameError or TypeError. An editor or linter will highlight unbalanced parentheses and inconsistent indent/variable names immediately.

Applying the above fixes will remove the SyntaxError and the most common runtime problems in the posted code.

Recommended Answers

All 2 Replies

You don't have the correct number of closing brackets to match the opening ones one the print lines.
Lines 22, 24 and 26 are all one short.

Also, the last 'else' should be another elif to work properly. That or remove the condition for that line.

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.