求解一个采购算法程序

2024-04-19 04:56:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我现在陷入困境

我正在创建一个程序来计算购买多个或单个商品时的完整销售额。该程序还应计算折扣阈值和购买时的州销售税。我可以让程序正常运行,但是,尽管有输入,我的最终结果是0.0美元。在这一点上,我可以确定它是乘以0,我假设这是税收投入,但我完全不知道如何纠正这个问题。下面是使用的代码

#declarations
A_STATE_TAX = float(.056)
C_STATE_TAX = float(.029)
N_STATE_TAX = float(.05125)
U_STATE_TAX = float(.047)
state = ''
tax = float()
completeSale = ()
sockPrice = int(5)
sandalPrice = int(10)
shoePrice = int(20)
bootPrice = int(30)
quantityShoes = int()
quantitySocks = int()
quantityBoots = int()
quantitySandals = int()
quantityTotal = int()
quantityTotal = int(quantityTotal)
basePriceSocks = (quantitySocks * sockPrice)
basePriceShoes = (quantityShoes * shoePrice)
basePriceBoots = (quantityBoots * bootPrice)
basePriceSandals = (quantitySandals * sandalPrice)
baseTotal = int(basePriceSocks + basePriceShoes + basePriceBoots +basePriceSandals)
discount = float()
discountAnswer = (baseTotal * discount)
purchaseWithoutTax = baseTotal - (baseTotal * discount)
taxAnswer = purchaseWithoutTax * tax

#mainbody
print("This algorithm will calculate your purchase.")

#housekeeping()
print("How many shoes do you wish to purchase?")
input(quantityShoes)
print("How many socks?")
input(quantitySocks)
print("Boots?")
input(quantityBoots)
print("And sandals?")
input(quantitySandals)

#purchaseinfo()
quantityTotal = (quantityShoes + quantityShoes + quantityBoots + quantitySandals)
if quantityTotal < 6:
    discount = 0
elif quantityTotal > 6 and quanityTotal < 10:
    discount = .10
else:
    discount = .20
purchaseWithoutTax = baseTotal - (baseTotal * discount)

#stateTax()
print("Please choose the following state: Arizona, New Mexico, Colorado or Utah.")
input(str(state))
if state == "arizona":
      tax = A_STATE_TAX
elif state == "new mexico":
    tax = N_STATE_TAX
elif state == "colorado":
    tax = C_STATE_TAX
else:
    tax = U_STATE_TAX
completeSale = (purchaseWithoutTax * tax) - taxAnswer

#endOfJob()
print(format(completeSale, '.2f'))
print("Your total is ", format(completeSale, '.2f'), " dollars.")
print("Thank you for your patronage.")

2条回答

主要的问题是,您的baseTotal = 0最初。和baseTotal = 0,因为您的数量(例如,quantityShoes)最初是0。不应该使用int()初始化值,应该使用0,因为它更显式。将值与baseTotal相乘,最后得到0

正如另一个答案所提到的,您错误地使用了input。对于数字量,您应该将input的结果转换为floatint,因为input返回字符串。还应将输出保存为变量名

quantityShoes = int(input("How many shoes?"))

您可以使用字典来清理代码。这可能有助于调试。您可以使用存储数量(以及价格、税费等)的字典,而不是使用多个quantity___变量

state_taxes = {
    "arizona": 0.056,
    "colorado": 0.029,
    "new mexico": 0.05125,
    "utah": 0.047,
}

prices = {
    "sock": 5,
    "sandal": 10,
    "shoe": 20,
    "boot": 30,
}

input()与您使用它的方式不一样。在input()中的参数 在用户输入之前打印。你做了相当于:

quantityShoes = int()
print("How many shoes do you wish to purchase?")
input(quantityShoes)

第一行将quantityShoes设置为默认整数,即0。第二行打印该文本。第三行打印该数字并等待用户输入。您希望执行以下操作:

quantityShoes = int(input("How many shoes do you wish to purchase?"))

相关问题 更多 >