Python如何在另一个子例程中使用子例程的输入

时间:2016-06-22 17:07:36

标签: function variables input procedures routines

我在Python中需要解决一个问题,即用户输入变量,例如他们想要购买的商品数量。我把它作为def customer得到了一个输出'你想要多少个小部件'然后他们的输入再乘以10英镑的价格。这个位工作正常。

我想把他们的输入转移到另一个子程序来进行进一步的数学计算,例如税和总计。是否有一些关键词供我学习,所以我可以这样做?

到目前为止,这是我的代码:

def wall_1():
   height = int(input("Enter the height in metres of wall 1 : "))
   width = int(input("Enter the width in metres of wall 1 : "))
   wall_1_price = height * width
   price_all_walls(wall_1_price)
def wall_2():
   height = int(input("Enter the height in metres of wall 2 : "))
   width = int(input("Enter the width in metres of wall 2 : "))
   wall_2_price = height * width
   price_all_walls(wall_2_price)
def wall_3():
   height = int(input("Enter the height in metres of wall 3 : "))
   width = int(input("Enter the width in metres of wall 3 : "))
   wall_3_price = height * width
   price_all_walls(wall_3_price)
def wall_4():
   height = int(input("Enter the height in metres of wall 4 : "))
   width = int(input("Enter the width in metres of wall 4 : "))
   wall_4_price = height * width
   price_all_walls(wall_4_price)
def price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price): 
   print("The total price so far is : " + 
       str(wall_1_price + wall_2_price + wall_3_price + wall_4_price))


if __name__ == "__main__":

   wall_1()
   wall_2()
   wall_3()
   wall_4()
   price_all_walls()

2 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但这可能有所帮助。

def customer():
   number_items = int(input("Enter the number of items to buy : "))
   print("Widgets that you would like : £" + str(number_items * 10))
   total_price = number_items * 10
   further_maths(number_items, total_price) # Call to another subroutine further_maths with parameters number_items and total_price

def further_maths(number_items, total_price): # Subroutine definition
   print("The total number of items in another subroutine : " + str(number_items))
   print("The total price in another subroutine : " + str(total_price))

if __name__ == "__main__":
   customer()

在此代码中,子例程customer()内的参数被传递到另一个子例程Further_maths()。

答案 1 :(得分:0)

此代码示例将解决您的问题。 price_all_walls方法接受四个参数。在你的wall_1(),wall_2(),wall_3()和wall_4()方法中,你只用一个参数(墙的价格)来调用price_all_walls()。这将引发错误“函数定义不存在”。

定义函数时,函数原型与它相关联(尽管这个术语最常用于C和C ++编程语言),其中包括方法的名称和类型签名(参数类型 - >不适用于python,返回类型等)。当你调用方法price_all_walls()时,应该用四个参数调用它,因此你的代码可以修改如下:

def wall_1():
   height = int(input("Enter the height in meters of wall 1 :"))
   width = int(input("Enter the width in meters of wall 1:"))
   wall_1_price = height * width
   wall_2(wall_1_price)

def wall_2(wall_1_price):
   height = int(input("Enter the height in meters of wall 2:"))
   width = int(input("Enter the width in meters of wall 2:"))
   wall_2_price = height * width
   wall_3(wall_1_price, wall_2_price)

def wall_3(wall_1_price, wall_2_price)
   height = int(input("Enter the height in meters of wall 3:"))
   width = int(input("Enter the width in meters of wall 3:"))
   wall_3_price = height * width
   wall_4(wall_1_price, wall_2_price, wall_3_price)

def wall_4(wall_1_price, wall_2_price, wall_3_price):
   height = int(input("Enter the height in meters of wall 4:"))
   width = int(input("Enter the width in meters of wall 4:"))
   wall_4_price = height * width
   price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price)

def price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price):
   print("The total price so far is : " + str(wall_1_price + wall_2_price + wall_3_price + wall_4_price))

if __name__=="__main__":
   wall_1()

虽然这是一种非常低效的方式(没有好的程序员会建议这样做)。这个例子对手头的问题提供了一个很好的解释。

如果您想编码此问题,我建议您使用全局变量或按照以下代码中显示的方式执行:

def wall_1():
   height = int(input("Enter the height of wall 1 :"))
   width = int(input("Enter the width of wall 1 :"))
   wall_1_price = height * width
   return wall_1_price

def wall_2():
   height = int(input("Enter the height of wall 2:"))
   width = int(input("Enter the width of wall 2:"))
   wall_2_price = height * width
   return wall_1_price

def wall_3():
   height = int(input("Enter the height of wall 3:"))
   width = int(input("Enter the width of wall 3:"))
   wall_3_price = height * width
   return wall_3_price

def wall_4():
   height = int(input("Enter the height of wall 4:"))
   width = int(input("Enter the width of wall 4:"))
   wall_4_price = height * width
   return wall_4_price

def price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price):
   return wall_1_price + wall_2_price + wall_3_price + wall_4_price

if __name__=="__main__":
   wall_1_price = wall_1()
   wall_2_price = wall_2()
   wall_3_price = wall_3()
   wall_4_price = wall_4()
   print("The total price of the walls is : " + str(price_all_walls(wall_1_price, wall_2_price, wall_3_price, wall_4_price)))

虽然有人会建议最好的方法如下。声明一个函数wall_n(int,int),它将高度和宽度作为参数并返回墙的价格。这导致了模块化代码,并提供了可重用性。

def wall_n(height, width):
   wall_n_price = height * width 
   return wall_n_price

def price_all_walls(prices):
   total_price = 0
   for price in prices:
      total_price += price
   return total_price

if __name__=="__main__":
   number_walls = int(input("Enter the number of walls to build : "))
   wall_prices = []
   for i in range(number_walls):
      height = int(input("Enter the height of wall " + str(i) + " : "))
      width = int(input("Enter the width of wall " + str(i) + " : "))
      wall_prices.append(wall_n(height, width))
   print("The total price is : " + str(price_all_walls(wall_prices)))

我没有证明使用全局变量。你可以阅读它here

我希望这能回答你的问题。