1

我可以用不同的代码来减少这个 Python 源代码的重要性吗?该程序的重​​点是获取用户总金额并将其添加到运费中。运费由国家(加拿大或美国)和产品价格决定:加拿大 125.00 美元的产品运费为 12.00 美元。


input ('Please press "Enter" to begin')

while True: print('这将计算运费和您的总计。')

totalAmount = int(float(input('Enter your total amount: ').replace(',', '').replace('$', '')))
Country = str(input('Type "Canada" for Canada and "USA" for USA: '))

usa = "USA"
canada = "Canada"
lessFifty = totalAmount <= 50
fiftyHundred = totalAmount >= 50.01 and totalAmount <= 100
hundredFifty = totalAmount >= 100.01 and totalAmount <= 150
twoHundred = totalAmount

if Country == "USA":
    if lessFifty:
        print('Your shipping is: $6.00')
        print('Your grand total is: $',totalAmount + 6)
    elif fiftyHundred:
        print('Your shipping is: $8.00')
        print('Your grand total is: $',totalAmount + 8)
    elif hundredFifty:
        print('Your shipping is: $10.00')
        print('Your grand total is: $',totalAmount + 10)
    elif twoHundred:
        print('Your shipping is free!')
        print('Your grand total is: $',totalAmount)

if Country == "Canada":
    if lessFifty:
        print('Your shipping is: $8.00')
        print('Your grand total is: $',totalAmount + 8)
    elif fiftyHundred:
        print('Your shipping is: $10.00')
        print('Your grand total is: $',totalAmount + 10)
    elif hundredFifty:
        print('Your shipping is: $12.00')
        print('Your grand total is: $',totalAmount + 12)
    elif twoHundred:
        print('Your shipping is free!')
        print('Your grand total is: $',totalAmount)

endProgram = input ('Do you want to restart the program?')
if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
    break
4

2 回答 2

3

这是简化代码的核心。它在美国打印 100.00 美元的运费

totalAmount = 100

chargeCode = (int(100*(totalAmount+0.001))-1)/5000 #0 -- <=50, 1 -- 50.01-100, etc
if chargeCode > 3: chargeCode = 3

shipping = {}
shipping[("USA", 0)] = 6
shipping[("USA", 1)] = 8
shipping[("USA", 2)] = 10
shipping[("USA", 3)] = 0
shipping[("Canada", 0)] = 8
shipping[("Canada", 1)] = 10
shipping[("Canada", 2)] = 12
shipping[("Canada", 3)] = 0

print shipping[("USA", chargeCode)]

totalAmount+0.001用于避免浮点数的乐趣:

int(100*(81.85)) == 8184

在我的系统上返回True,因为 float point81.85比 decimal 少一点81.85

于 2013-01-31T04:46:04.937 回答
1

以下是成本计算的基本策略:

import math

amount = int(totalAmount)
assert amount >= 0
shipping = { 
  'USA': [6, 8, 10],
  'Canada': [8, 10, 12]
}
try:
    surcharge = shipping[country][amount and (math.ceil(amount / 50.0) - 1)]
except IndexError:
    surcharge = 0
total = amount + surcharge

The key notion here is that the the shipping cost ranges follow a fairly linear progression: [0-50], (50-100], (100-150], (150, inf)

Note that the first group is a little funny, as it includes the lower bound of 0 where the other groups don't include the lower bound (they are an open interval at the bottom). So going forward we'll consider the first group as the following: 0 or (0-50]

We want to transform the amount the user supplies into an index into the shipping cost lists [6, 8, 10] and [8, 10, 12]. The lists are of length 3, so the indexes are 0, 1 and 2. Notice that if we divide any number in the range (0, 150] by 50.0 (we add the .0 to 50 so we a get a real number back -- 1 / 50 == 0 but 1 / 50.0 == 0.02 -- for the next step) we get a number in the range (0 and 3].

现在意识到 math.ceil 会将实数四舍五入到大于或等于自身的最接近的整数 - 例如。math.ceil(.001) == 1.0, math.ceil(1.5) == 2.0, math.ceil(2) == 2.0. 因此,将 math.ceil 应用于 (0, 3] 范围内的数字,我们将提供 1.0、2.0 或 3.0。将这些数字转换为 int ( int(2.0) == 2),我们得到值 1、2 和 3。从这些值中减去 1,我们得到 0、1、2。瞧!这些数字与我们运输数组中的索引匹配。

你可以用 Python 来表达这种转换:int(math.ceil(amount / 50.0) - 1)

We are almost there. We've handled any amount in the range (0, 150]. But what if amount is 0. math.ceil(0) == 0.0 and 0.0 - 1 == -1.0 This will not index properly into our array. So we handle 0 separately by checking first if amount is equal to 0, and if it is, using 0 as our shipping array index instead of the applying our transformation to amount to determine the index. This can be accomplished using Python's short circuit and operator (the web should have plenty of information on this) in the following expression: amount and int(math.ceil(amount / 50.0) - 1)

Note that any value above 150 will reduce to an index greater than 2 and applying that index against the shipping array will result in an IndexError. But any value greater than 150 has free shipping so we can catch the IndexError and apply a surcharge of 0:

try:
    surcharge = shipping[country][amount and int(math.ceil(amount / 50.0) - 1)]
except IndexError:
    surcharge = 0

And we're done!

于 2013-01-31T05:04:39.053 回答