0

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()
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
A.Teacher
  • 11
  • 3
  • Do not describe your broken code, post it. If you knew enough about what was wrong to describe it correctly, you would not be asking for help. Any keywords we give you would be based on incorrect assumptions. On the other hand, if you ask a proper question, you may get a better response than you expected. – Mad Physicist Jun 22 '16 at 17:16
  • Please post the existing working / broken code for added clarity. – Aditya Jun 22 '16 at 17:24

2 Answers2

0

I am not sure if I have understood your question correctly, but this may help.

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

In this code the parameters inside the subroutine customer() are passed into another subroutine further_maths().

Aditya
  • 1,172
  • 11
  • 32
0

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.

Community
  • 1
  • 1
Aditya
  • 1,172
  • 11
  • 32
  • You are amazing, thank you so much for taking time out to help me. Bless you! – A.Teacher Jun 23 '16 at 17:01
  • Happy to help. Thanks. – Aditya Jun 23 '16 at 17:02
  • You can mark this as an answer then. Other people can benefit from this. – Aditya Jun 23 '16 at 17:06
  • @A.Teacher You are asking newbie style python questions, which is good, but you are phrasing your questions like a 5 year old phrases a question about higher maths, it makes us veteran coders roll our eyes and say you need to visit the fundamentals, one way to do this is to go to http://codingbat.com/python and do every problem set, and every exercise until you get an intuitive feel for how python functions work, how parameter types work, and how you can setup chains of routines. You're going to get downvotes on stackoverflow if you post these very-low-level questions. – Eric Leschinski Jun 24 '16 at 14:36
  • Yes, I would love to find the time to do this. Computer science and Python programming was sprung on us, and I have until September to teach myself, whilst holding down a full time job. Sorry if my questions irritate you. Let's hope you are never in the position where you have to learn a whole new language in limited time, under pressure, and if you do find you are, you find supportive helpful people, and not one who treats you like an idiot. Thanks for the link, I have hundreds of links. – A.Teacher Jun 24 '16 at 16:16
  • @A.Teacher That sounds scary! Maybe there's a computer club in your area where you could get some serious one-on-one programming / Python mentoring. Stack Overflow can be great, but there are limits to what we can do here. Anyway, best of luck! – PM 2Ring Jun 26 '16 at 11:20