在 Python 中传递参数
Passing Arguments in Python
我想弄清楚如何在 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()
在main
中,调用getNumDoughnuts
时确实没有定义numDougnuts
。 OTOH,后一个函数忽略了它的参数并且 returns 是一个值,main
又忽略了它。依此类推——您需要将参数与 return 值区分开来!
所以按正确的顺序排列你的程序将变成:
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()
看到参数和 return 值之间的区别了吗?参数是使 进入 函数的原因(并且它们的值必须在您调用该函数时定义)。 return 值是函数的 out 值——并且通常需要绑定到变量,或者由函数的调用者以其他方式使用。
还有,当然,你需要调用main
,否则什么也不会发生!-)
我想弄清楚如何在 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()
在main
中,调用getNumDoughnuts
时确实没有定义numDougnuts
。 OTOH,后一个函数忽略了它的参数并且 returns 是一个值,main
又忽略了它。依此类推——您需要将参数与 return 值区分开来!
所以按正确的顺序排列你的程序将变成:
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()
看到参数和 return 值之间的区别了吗?参数是使 进入 函数的原因(并且它们的值必须在您调用该函数时定义)。 return 值是函数的 out 值——并且通常需要绑定到变量,或者由函数的调用者以其他方式使用。
还有,当然,你需要调用main
,否则什么也不会发生!-)