Solving a purchasing algorithm program

108 Views Asked by At

So, I'm stuck at the moment.

I'm creating a program to calculate the complete sale if multiple, or singular, items are purchased. The program is also supposed to calculate a discount threshold and a state-sale's tax with the purchase. I can get the program to function, however, my end result is 0.0 dollars despite entries made. At this point, I can identify it is multiplying SOMETHING by 0, which I assume is the tax input, but I am at a total loss on how to correct this issue. Below is the code used.

#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

There are 2 best solutions below

0
On

The main issue is that your baseTotal = 0 initially. And baseTotal = 0 because your quantities (e.g., quantityShoes) are initially 0. You shouldn't initialize values with int(), you should use 0 instead because it is more explicit. You multiply values with baseTotal so in the end you will get 0.

And as another answer mentions, you are using input incorrectly. For numeric quantities, you should convert the result of input to float or int, because input returns strings. You should also save the output to a variable name.

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

You can clean up your code by using dictionaries. That might help with debugging. Instead of having multiple quantity___ variables, you can use a dictionary that stores quantities (and prices, taxes, etc.).

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

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

input() doesn't work the way you used it. The argument that goes in input() is printed before the user gives input. You did the equivalent of:

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

The first line sets quantityShoes equal to the default integer, which is 0. The second line prints that text. The third line line prints that number and waits for user input. You want to do something like:

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