Search code examples
pythonbilling

How to make a bill by Python


I'm a freshman, and I want to make a bill that write out what I bought, quantity and how much total cash it cost.

customer = str(input("Customer's name: "))
print ("Welcome to our store")
print ("This is our products")
print ("orange 0.5$)
print ("Apple 1$)
print ("Grape 0.5$)
print ("Banana 0.5$)
chooseproducts= int(input("What do you want to buy: "))

For output example. Can you guys help me please. 
    

         BILL
Orange     5    2.5$
Apple      3    3$
Grape      2    1$
Total:     10   6.5$

Solution

  • First, your str(input(customer's name: )) code needs to be changed. input() calls need a string as the question, and also the apostrophe has Python think that it is the start of a string. Also, when you are printing the costs, you need another closing quotation mark. When you are asking the user about what they want to buy, I suppose you want them to input the name of the fruit. Since you have the int() in the front, Python cannot turn a string like "orange" or "grape" into an integer. When people want to buy multiple things, however, they might only put one thing in. I suppose that this isn't all your code, and you have the part where you calculate the cost. If I were to write this code, I would write it like the following:

    customer = str(input("Customer's Name:"))
    print ("Welcome to our store")
    print ("This is our products")
    print ("Orange 0.5$")
    print ("Apple 1$")
    print ("Grape 0.5$")
    print ("Banana 0.5$")
    prices = {"orange":0.5,"apple":1,"grape":0.5,"banana":0.5}# I added this so we can access the prices later on
    #chooseproducts= int(input("What do you want to buy: "))
    # The code below this is what I added to calculate the cost.
    productnum = input("How many things do you want to buy: ") # This is for finding the number of different fruits the buyer wants
    while not productnum.isdigit():
        productnum = input("How many different products do you want to buy: ")
    productnum = int(productnum)
    totalprice = 0
    totalfruit = 0
    print('         BILL')
    for i in range(productnum):
        chosenproduct = input("What do you want to buy: ").lower()
        while not chosenproduct in ['orange','apple','banana','grape']:
            chosenproduct = input("What do you want to buy: ").lower()
        fruitnum = input("How many of that do you want to buy: ")
        while not fruitnum.isdigit():
            fruitnum = input("How many of that do you want to buy: ")
        fruitnum = int(fruitnum)
        totalfruit += fruitnum
        price = fruitnum * prices[chosenproduct]
        totalprice += price
        startspaces = ' ' * (11 - len(chosenproduct))
        endspaces = ' ' * (5 - len(str(fruitnum)))
        print(chosenproduct.capitalize() + startspaces + str(fruitnum) + endspaces + str(price) + '$')
    
    print('Total:     ' + str(totalfruit) + ' ' * (5 - len(str(totalprice))) + str(totalprice) + '$')
    

    Please make sure you understand the code before copying it, thanks!