0

I am trying to write a program that calculates shipping costs for an imaginary courier and am trying to work with exception handling.

MetricOrImperial = input ("Are you using centimetres and kilograms or inches and pounds? (CK/IP)")

if MetricOrImperial == "CK":
  dimensionsCW = input ("What is the width of your package?")
  dimensionsCL = input ("What is the length of your package?")
  dimensionsCH = input ("What is the height of your package?")
  dimensionsW = int(dimensionsCW)/2.54
  dimensionsL = int(dimensionsCL)/2.54
  dimensionsH = int(dimensionsCH)/2.54
  weightCK = input ("What is the weight of your package?")
  weight = int(weightCK)/2.205
elif MetricOrImperial == "IP":
  dimensionsW = input ("What is the width of your package?")
  dimensionsL = input ("What is the length of your package?")
  dimensionsH = input ("What is the height of your package?")
  weight = input ("What is the weight of your package?")
elif MetricOrImperial != ("CK" or "IP"):
  print ("You entered an incorrect response.")

shippingSpeed = input("What is your desired shipping speed in days? (Enter number between 1 - 5)")

In the exception handling I am trying to make the user go back to the first line ("MetricOrImperial") and restart the process, if the user has typed something that is neither "CK" nor "IP". The expected output is that it goes back to said line, but I'm not sure how to go about doing that.

Andrew Wang
  • 194
  • 10

1 Answers1

0

You don't really need exception handling (try/catch statements) in your case, you can just do a simple while condition:

MetricOrImperial = ""

while (MetricOrImperial != "CK" or MetricOrImperial != "IP"):
  MetricOrImperial = input ("Are you using centimetres and kilograms or inches and pounds? (CK/IP) ")

  if MetricOrImperial.upper() == "CK":
    dimensionsCW = input ("What is the width of your package?")
    dimensionsCL = input ("What is the length of your package?")
    dimensionsCH = input ("What is the height of your package?")
    dimensionsW = int(dimensionsCW)/2.54
    dimensionsL = int(dimensionsCL)/2.54
    dimensionsH = int(dimensionsCH)/2.54
    weightCK = input ("What is the weight of your package?")
    weight = int(weightCK)/2.205
  elif MetricOrImperial.upper() == "IP":
    dimensionsW = input ("What is the width of your package?")
    dimensionsL = input ("What is the length of your package?")
    dimensionsH = input ("What is the height of your package?")
    weight = input ("What is the weight of your package?")
  else:
    print ("You entered an incorrect response.")

shippingSpeed = input("What is your desired shipping speed in days? (Enter number between 1 - 5)")

Note 1: Notice I used upper() so we allow the user to type the answer in lowercase and then convert it to uppercase when comparing.

Note 2: When you want to check if anything else than your previous checked conditions happened you should use an else statement.

Note 3: MetricOrImperial != ("CK" or "IP") won't have the behavior you expected, in boolean logic when you write ("CK" or "IP") that expression will have a value either true or false and that is what is going to be compared with MetricOrImperial !=. For your desired behavior you'd have to explicitly write something like not(MetricOrImperial == "CK" or MetricOrImperial "IP").

Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35