functionvariablesinputproceduresroutines

Python how to use inputs from sub routines in another sub routine


I have a problem to solve in Python whereby the user inputs variables such as number of items they want to buy. I've got this as a def customer, with an output 'how many widgets would you like' and then their input multiplied by a price of say £10. This bit works fine.

I want to take that input of theirs to another subroutine to do further maths on, such as tax, and total. Are there some keywords for me to study so I can do this?

Here is my code so far:

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()

Solution

  • This code sample will solve your problem. The price_all_walls method accepts four arguments. In your wall_1(), wall_2(), wall_3() and wall_4() methods, you are calling the price_all_walls() with only one argument (the price of the wall). This will throw an error "the function definition does not exist".

    When you define a function, a function prototype is associated to it (although this term is most commonly used in C and C++ programming languages), which includes the method's name and the type signature (parameter types -> not applicable in python, return type etc.). When you call the method price_all_walls(), it should be called with four arguments and hence your code could be modified as follows:

    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()
    

    Although this is a very inefficient way of doing this (no good programmer will ever suggest this). This example provides a good explanation to the question at hand.

    If you would want to code this problem, I'd suggest you make use of a global variable or do it the way shown in the code below:

    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)))
    

    Although anyone would suggest that the best way to do this would be as follows. Declare a function wall_n(int, int) that takes the height and width as arguments and returns the price of the wall. This leads to modular code and also provides re usability.

    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)))
    

    I have not demonstrated the use of global variables. You can read about it here

    I hope this answers your question.