简体   繁体   English

当用户在 Python 中询问时,我如何回忆整个 function?

[英]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.

相关问题 我如何从列表中随机选择用户要求在 python 中使用输入的次数? - how do i randomly pick characters out of a list the amount of times the user asks for using input in python? Python,我如何制作一个倒数时钟,询问用户他们要从哪个数字开始 - Python, how do I make a countdown clock that asks the user what number they want to start at Python - 如何调用正确的函数 - Python - How to recall the correct function 如何为要求用户输入的 function 创建单元测试 - How can i create a unit test for a function that asks user for input 如何导出和调用 Python 函数(Pickle、Joblib、Dump)? - How Can I Python Function Export and Recall (Pickle,Joblib,Dump)? Python 初学者 - 如何从列表中召回已删除的项目 - Python Beginner - How do I recall the deleted items from a list 如何从 describe() 函数在 Python 中打印整数? - How do I print entire number in Python from describe() function? Python函数在调用时获得不同的值 - Python Function Get Different Value When Recall 我想在 vs 代码中调用一个 python 函数,但总是发生错误。 我该如何解决? - I want to recall a python function in vs code but always an error happens. how can I solve it? 如何使用tkInter显示一个询问用户多项选择题的对话框? - How do I display a dialog that asks the user multi-choice question using tkInter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM