Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Python Programming HELP, why won't my loop end when the input is N?

####################################

#Calculate the parcel delivery cost

#####################################

# ParcelCost.py written by me

# loop

while 1:

# Get the parcel weight

parcel_weight = float(raw_input ("Please enter the parcels weight:"))

# Calculate the delivery cost

if parcel_weight <2.5: delivery_cost = parcel_weight* 3.50

if parcel_weight >=2.5 <=5: delivery_cost = parcel_weight* 2.85

if parcel_weight >5: delivery_cost = parcel_weight* 2.45

# Output the results

print "The delivery cost is:$", delivery_cost

# Repeat

response ==(raw_input("Would you like to do another? Y or N:"))

if response == "Y" or "y": continue

else:

break

Update:

Thanks Silent...I see your point, but the loop still continues if I enter a N or n.???

1 Answer

Relevance
  • Silent
    Lv 7
    1 decade ago
    Favorite Answer

    Look at this line:

    if response == "Y" or "y": continue

    You're testing two things for truth value:

    1. response == "Y"

    2. "y"

    In Python, a non-empty string is always true, so "y" is always true.

    What you want is something like this:

    if response == "Y" or response == "y"

Still have questions? Get your answers by asking now.