Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中传递参数_Python - Fatal编程技术网

在Python中传递参数

在Python中传递参数,python,Python,我试图弄清楚如何在Python中传递参数,以及为什么我的代码不能工作。为什么说没有定义参数?我需要每个函数中的参数,以便能够相互对话。我只能通过将变量放入定义的主函数中来解决这个问题,这不是我想要设计程序的方式 #make computer program that takes amount of doughnuts that customer #wants to order. #second, multiply the number of doughnuts the customer want

我试图弄清楚如何在Python中传递参数,以及为什么我的代码不能工作。为什么说没有定义参数?我需要每个函数中的参数,以便能够相互对话。我只能通过将变量放入定义的主函数中来解决这个问题,这不是我想要设计程序的方式

#make computer program that takes amount of doughnuts that customer
#wants to order.
#second, multiply the number of doughnuts the customer wants to order
#by the price and the sales tax.
#next, print the price of the doughnut with the sales tax
DOUGHNUT=1.75
SALES_TAX=0.08625

def getNumDoughnuts(numDoughnuts):
    numDoughnuts= raw_input("How many donuts would you like to order?")
    return numDoughnuts

def calculateDoughnutPrice(numDoughnuts, doughnutPrice):
    doughnutPrice=DOUGHNUT*float(numDoughnuts)
    return doughnutPrice

def calculateSalesTax(doughnutPrice, priceWithTax):
    taxAmount= (doughnutPrice*(SALES_TAX))
    priceWithTax= taxAmount+doughnutPrice
    return priceWithTax

def displayPrice(priceWithTax):
    print(priceWithTax)

def main():
    getNumDoughnuts(numDougnuts)
    calculateDoughnutPrice(numDoughnuts, doughnutPrice)
    calculateSalesTax(doughnutPrice, priceWithTax)
    displayPrice(priceWithTax)

main()
大体上,当您调用getNumDoughnuts时,确实没有定义numDougnuts。OTOH,后一个函数忽略其参数并返回一个值,main反过来忽略该值。等等-您需要区分参数和返回值

因此,将事情按正确的顺序排列您的程序将变成:

DOUGHNUT = 1.75
SALES_TAX = 0.08625

def getNumDoughnuts():
    numDoughnuts = raw_input("How many donuts would you like to order?")
    return numDoughnuts

def calculateDoughnutPrice(numDoughnuts):
    doughnutPrice = DOUGHNUT * float(numDoughnuts)
    return doughnutPrice

def calculateSalesTax(doughnutPrice):
    taxAmount = doughnutPrice*(SALES_TAX)
    priceWithTax = taxAmount + doughnutPrice
    return priceWithTax

def displayPrice(priceWithTax):
    print(priceWithTax)

def main():
    numDoughnuts = getNumDoughnuts()
    doughnutPrice = calculateDoughnutPrice(numDoughnuts)
    priceWithTax = calculateSalesTax(doughnutPrice)
    displayPrice(priceWithTax)

main()
查看参数和返回值之间的差异?参数是进入函数的内容,它们的值必须在调用该函数时定义。返回值是从函数中得到的,通常需要绑定到变量,或者由函数的调用方使用


当然,您还需要调用main,否则什么也不会发生-

您应该了解如何使用面向对象编程,特别是在Python中。因此,您可以使用带有变量和访问器的类,您应该看看如何格式化以@daouzli开头的Python代码。这个问题实际上与OOP无关。正如Alex指出的,海报在区分参数和返回值时遇到了困难。他们需要先了解这个基本概念,然后才能开始OOP。感谢所有的帮助。我学习Python已经有一段时间了,但多亏了你的帖子,我现在开始记住一些东西了。我试图通过将值放在括号中与每个函数进行通信,但现在请记住,您必须使用等号在main中访问它们?谢谢!