Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

EDIT -- I just saw that the solutions are all there when you press submit, oops

Hi!

Can anyone tell me the error in this code? Since this isn't an assignment, I'll post it here. When typing in a weight, I currently get infinite "very heavy"'s printed. Thank you in advance for any help ^^

def analyzeWeight(weight):

while weight != -1:
    if weight >= 70:
        print("very heavy")
    elif weight >= 10 and weight < 70:
        print("heavy")
    elif weight >= 2 and weight < 10:
        print("light")
    elif weight < 2 and weight > 0:
        print("very light")
    print()
    weight = float(input("Package weight (-1 to quit): "))
if weight == -1:
    print("Thanks for using weight analyzer")
print()
    

def main():

weight = float(input("Package weight (-1 to quit): "))
analyzeWeight(weight)

main()

asked in CSC201 Spring 2021 by (1 point)
edited by

1 Answer

+3 votes
 
Best answer

One issue might be that you have an elif for when weight is -1, but you can't print it because it's in a while loop for every weight not equal to -1.

The infinite loop would be because you don't ask for another input within the while loop, so it just repeats with the same weight over and over.

answered by (1 point)
selected by
...