简体   繁体   中英

How do I recall an entire function when the user asks for it in Python?

I'm new to Python and I'm trying to figure out how to create and if loop to restart an entire function (in this case, the programOutput function) if the user enters "Y". Also if my use of Class and def are totally wrong then I am welcoming to help and constructive criticism. Thanks!

import math


class userEntry:
    userName = input("Enter Customer Name: ")
    userQuantityPurchased = int(input("Enter Quantity Purchased: "))
    userPrice = int(input("Enter Price per Unit: "))

class taxCalculator:
    stateTax = .09
    countyTax = .05
    discountRate = .1
    salesAmount = float(str(userEntry.userQuantityPurchased * userEntry.userPrice))
    stateCalculator = str(salesAmount * stateTax)
    countyCalculator = str(salesAmount * countyTax)
    totalTax = sum = float(stateCalculator) + float(countyCalculator)
    grossSales = sum = float(salesAmount) - float(totalTax)
    discount = sum = float(discountRate) * float(salesAmount)
    netSales = sum = float(grossSales) - float(discount)

    programOutput()
    

def programOutput():

    print("---------------------------------------")
    print("Here is your Net Sale!")
    print("---------------------------------------")
    print("Customer Name:  ", userEntry.userName)
    print("Sales Amount: ", "$", taxCalculator.salesAmount)
    print("Total Taxes: ", "$", taxCalculator.totalTax)
    print("---------------------------------------")
    print("\n")
    print("Gross Sales: ", "$", taxCalculator.grossSales)
    print("Discount: ", "$", taxCalculator.discount)
    print("\n")
    print("---------------------------------------")
    print("\n")
    print("Net Sales: ", "$", taxCalculator.netSales)
    print("---------------------------------------")
    print("---------------------------------------")
    print("\n")
    print("Thank you for your business!")
    print("\n")
    nextSale = input("Do you want to process another sale? (Y/N):   ")

    

    if nextSale == 'Y':
        programOutput()

Use a loop, not recursion.

And you need to put the code that asks for input in the loop, otherwise you'll just keep printing the same values.

def programOutput():
    print("---------------------------------------")
    print("Here is your Net Sale!")
    print("---------------------------------------")
    print("Customer Name:  ", userName)
    print("Sales Amount: ", "$", salesAmount)
    print("Total Taxes: ", "$", totalTax)
    print("---------------------------------------")
    print("\n")
    print("Gross Sales: ", "$", grossSales)
    print("Discount: ", "$", discount)
    print("\n")
    print("---------------------------------------")
    print("\n")
    print("Net Sales: ", "$", netSales)
    print("---------------------------------------")
    print("---------------------------------------")
    print("\n")
    print("Thank you for your business!")
    print("\n")

while True:
    userName = input("Enter Customer Name: ")
    userQuantityPurchased = int(input("Enter Quantity Purchased: "))
    userPrice = int(input("Enter Price per Unit: "))

    stateTax = .09
    countyTax = .05
    discountRate = .1
    salesAmount = float(str(userEntry.userQuantityPurchased * userEntry.userPrice))
    stateCalculator = str(salesAmount * stateTax)
    countyCalculator = str(salesAmount * countyTax)
    totalTax = sum = float(stateCalculator) + float(countyCalculator)
    grossSales = sum = float(salesAmount) - float(totalTax)
    discount = sum = float(discountRate) * float(salesAmount)
    netSales = sum = float(grossSales) - float(discount)

    programOutput()

    nextSale = input("Do you want to process another sale? (Y/N):   ")
    if nextSale.lower() == 'n':
        break

There's no need for the classes, just set ordinary variables. Or you could define functions that return the values, and assign them in the loop.

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