0

This is the code I have so far:

def convertWeight():
   (totalWeight * 0.45359237)

def getRate():
   if totalWeight <= 2:
       rate = totalWeight * 1.10
   elif totalWeight > 2 and totalWeight <= 6:
       rate = totalWeight * 2.20
   elif totalWeight > 6 and totalWeight <= 10:
       rate = totalWeight * 3.70
   elif totalWeight > 10:
       rate = totalWeight * 4.20

numBoxes = int(input('Please enter the number of boxes: '))
unit = input('Please enter the unit of measurement, Imperial or Metric (as I or M): ')
while unit != 'I' or 'M' or 'i' or 'm':
    unit = input('Please enter the unit of measurement again, Imperial or Metric (as I or M): ')
while unit == 'I' or 'M' or 'i' or 'm':
    for x in range(numBoxes):
        weight = float(input('Please enter the weight of the boxes: '))
        totalWeight = 0
        totalWeight = totalWeight + weight
        if unit == 'M':
            totalWeight = convertWeight
        else:
            totalWeight = totalWeight
getRate(totalWeight)
transportCost = getRate * totalWeight
print('The number of boxes is {0}, the total weight is {1:.2f}, and the transport cost is {2:,}.' .format(numBoxes, totalWeight, transportCost))

How do I firstly, loop unit input until I, i, M, or m, are inputted, and then break the loop and continue? And how do I secondly, calculate the transport cost, that is, get the rate from the function created, and then times it by totalWeight?

Thanks everyone.

Nick
  • 63
  • 2
  • 9
  • get rid of the second while loop. It is unnecessary since you know that unit will be `I,i,M,m` because you just checked for it. You also know the number of boxes. – SirParselot Feb 12 '16 at 13:08

1 Answers1

0

There's a lot wrong with this program. Firstly, your second while loop serves no purpose and causes extra problems because now you have to break out of it. Second, you are resetting totalweight in each iteration of your for loop. Third, you are trying to call convertWeight() but instead you are setting totalweight equal to it instead. Fourth, you try to call getRate() but since you left off the () your program will try to evaluate it as a variable and not a function and throw an error. Fifth, totalWeight is not in the functions scope which means you have to pass it in as a parameter. Sixth, your while loop to check for proper input isn't going to work as expected. It is going to evaluate if unit != 'I' then it is going to evaluate whether or not 'M' is True, which it is. Your while loop will always evaluate to True. Seventh, for metric you are converting the weight in each iteration of the loop and then on the next loop you add imperial to it and then convert it again. This will give you the wrong answer. Move your if statement outside of the for loop and remove the else since it does nothing. Lastly, convertWeight() and getRate() essentially do nothing because you don't return anything. You can fix it like this.

def convertWeight(totalWeight):
   return (totalWeight * 0.45359237)

def getRate(totalWeight):
   if totalWeight <= 2:
       rate = totalWeight * 1.10
   elif totalWeight > 2 and totalWeight <= 6:
       rate = totalWeight * 2.20
   elif totalWeight > 6 and totalWeight <= 10:
       rate = totalWeight * 3.70
   elif totalWeight > 10:
       rate = totalWeight * 4.20

   return rate

numBoxes = int(input('Please enter the number of boxes: '))
unit = input('Please enter the unit of measurement, Imperial or Metric (as I or M): ')
while unit not in ['I','M','i','m']:
    unit = input('Please enter the unit of measurement again, Imperial or Metric (as I or M): ')

totalWeight = 0
for x in range(numBoxes):
    totalWeight += float(input('Please enter the weight of the boxes: '))

if unit == 'M':
    totalWeight = convertWeight(totalWeight)

transportCost = getRate(totalWeight) * totalWeight
print('The number of boxes is {0}, the total weight is {1:.2f}, and the transport cost is {2:,}.' .format(numBoxes, totalWeight, transportCost))
SirParselot
  • 2,640
  • 2
  • 20
  • 31
  • I have implemented your suggestions, but now the code just infinitely loops the 'Enter the unit of measurement again' statement, never moving on, no matter what I input. – Nick Feb 12 '16 at 13:23