简体   繁体   中英

How do I call this function multiple times?

#calculates total price of your items as your shopping

print("The sales tax in Lucas County is 7.25%")
sales_tax = 0.0725
price_total=0

price = float(input("Insert price of item "))
price_tax= price * sales_tax
price_semitotal = price_tax + price
print("Price of that item with tax: ", price_semitotal)
price_total+=price_semitotal
print("Total Price: ", price_total)

How do I change this so that it loops and keeps prompting the user to add more items? I want it to be like a grocery list where you input the price of one item, it calculates the tax for it so that you can see the final price of that one item and the final price of all of your items together. I also tried something like this but it didn't work:

print("The sales tax in Lucas County is 7.25%")
sales_tax = 0.0725
price_total=0
def price(price_input, price_tax, price_semitotal, price_total): 
    price_input = float(input("Insert price of item "))
    return price_tax = price_input * sales_tax
    return price_semitotal = price_tax + price
    print("Price of that item with tax: ", price_semitotal)
    return price_total+=price_semitotal
    print("Total Price: ", price_total)

while True:
    price
print("The sales tax in Lucas County is 7.25%")
price_total=0
sales_tax = 1.0725
def price():
    price_input = float(input("Insert price of item "))

    price_tax = price_input * sales_tax
    return price_tax

while True:
    x = price()
    print(f"Price of that item with tax: {x:.2f}")
    price_total += x
    print(f"Total Price: {price_total:.2f}")
    if x == 0:
        break

sales_tax = 1.0725 está substituindo

return price_tax = price_input * sales_tax
return price_semitotal = price_tax + price

{x:.2f} usei para usar apenas duas casas decimais.

Sua base na linguagem não está boa, procure vídeos e livros para melhorar.

Quando price_input for igual a 0 o loop acaba.

This is the correct code for this but this really isn't where you should ask this sort of thing, Stack Overflow is for questions unanswered in documentation etc. What you've asked really can be picked up from documentation and tutorials

print("The sales tax in Lucas County is 7.25%")
def price(): 
    sales_tax = 0.0725
    price_total=0
    price_input = float(input("Insert price of item "))
    price_tax = price_input * sales_tax
    price_semitotal = price_tax + price_input
    print("Price of that item with tax: ", price_semitotal)
    price_total+=price_semitotal
    print("Total Price: ", price_total)

while True:
    price()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM