[英]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".我是 Python 的新手,我试图弄清楚如果用户输入“Y”,如何创建和 if 循环以重新启动整个 function(在本例中为 programOutput 函数)。 Also if my use of Class and def are totally wrong then I am welcoming to help and constructive criticism.
此外,如果我对 Class 和 def 的使用完全错误,那么我欢迎提供帮助和建设性的批评。 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.
或者您可以定义返回值的函数,并在循环中分配它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.