Home > database >  my program keeps looping and i dont know why
my program keeps looping and i dont know why

Time:09-24

the user is required to input package weight and after I tried to input the packages it gives me the correct math but the answer keeps on repeating.

heres the code:

more = "y"

count = 0

total = 0 x = 0

while(more == "y"): lbs = eval(input("Please enter the weight of the package: "))

if (lbs >= 1 and lbs <= 2):
    op1:(lbs * 1.10)
    x = op1
    count  = 1
    total  = x
    print("The current package shipping cost is:",op1)

if (lbs > 2 and lbs <= 6):
    op2 = (lbs * 2.20)
    x = op2
    count  = 1
    total  = x
    print("The current package shipping cost is:",op2)

if (lbs > 6 and lbs <= 10):
    op3 =(lbs * 3.70)
    x = op3
    count  = 1
    total  = x
    print("The current package shipping cost is:",op3)


if (lbs > 10):
    op4 =(lbs * 3.80)
    x = op4
    count  = 1
    total  = x
    print("The current package shipping cost is:",op4)

if (lbs == 0):
    print("your package cannot be delivered")

more = input ("Do you want to enter another Package? <y/n>: ")

while(more == "n"): print("Your total shipping cost is:",total,"for",count,"packages")

CodePudding user response:

why don't you just start with

while True:

and place a break after each if evaluation, then you get only correct input, and in case of incorrect input the while loop starts again with the input question. The input question also requires the information that the weight needs to be in lbs.

CodePudding user response:

This line -

while(more == "n"):
     print("Your total shipping cost is:",total,"for",count,"packages")

You do not need the while loop. Just do -

print("Your total shipping cost is:",total,"for",count,"packages")

Additionally, you do not need eval(), just use int() -

lbs = int(input("Please enter the weight of the package: "))

You could use range() function instead of the >= <= ... etc. things. For e.g. -

if lbs in range(1,2 1): # Not really useful, still you could use this
    # code

It is not significant, so to keep it simple, you do not need to use the range function

  • Related