입력 한 영국식 측정 값을 미터법으로 변환하려고하는데 변환이 올바르지 않고 그 이유를 잘 모르겠습니다. 예를 들어, 50 파운드의 총 중량 영국식 단위 입력을 합하면 출력은 17.72kg이됩니다. 조언, 비평가 또는 코드 변경을 환영합니다. 이것은 내 코드입니다.
# Convert the total weight from Imperial, to Metric units
def convertWeight(totalWeight):
return (totalWeight * 0.45359237)
# Calculate the cost of transport, by multiplying certain weight conditions by their respective cost per kilograms
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
# Get the number of boxes
numBoxes = int(input('Please enter the number of boxes: '))
# Get the unit of measurement
unit = input('Please enter the unit of measurement, Imperial or Metric (as I or M): ')
# If the inputted unit of measurement does not equal one of these conditions, ask again for the unit of measurement, until one of these characters are inputted.
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 each box, get their respective weight
for x in range(numBoxes):
weight = float(input('Please enter the weight of the boxes: '))
# Sum up the total weight by adding the inputted weights
totalWeight = totalWeight + weight
# Does not work, check Parlas answers ; If the inputted unit is Imperial, convert it to Metric
if unit in ['I', 'i']:
totalWeight = convertWeight(totalWeight)
else:
totalWeight = totalWeight
# Calculate the transport cost, by calling the rate function
transportCost = getRate(totalWeight)
# Output the number of boxes, the total weight, and the transport cost to the user
print('The number of boxes is {0}, the total weight is {1:.2f} kilograms, and the transport cost is ${2:,.2f}.' .format(numBoxes, totalWeight, transportCost))
문제는 임페리얼 체중 변환이
if unit in ['I', 'i']:
totalWeight = convertWeight(totalWeight)
get-weights 루프 안에 있습니다.
for x in range(numBoxes):
weight = float(input('Please enter the weight of the boxes: '))
# Sum up the total weight by adding the inputted weights
totalWeight = totalWeight + weight
따라서 (예를 들어) 각각 무게가 25 파운드 인 2 개의 상자가있는 경우 미터법 무게는이어야 (25 + 25) * 0.4536
하지만 대신 계산하는 것 ((25 * 0.4536) + 25) * 0.4536
입니다.
모든 가중치를 얻은 후에 만 변환이 발생하는지 확인하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다