-3

Can someone explain to me how I can use the input from one function in another? I have seen different questions but it doesn't make any sense to me: What is the purpose of the return statement?

I am trying to use the input here as the sides of a triangle. The next function should get that input and use it to get the perimeter. I want to be able to use those three sides later on in another function. This is what I have:

 #Input Func. using map
 def u_input_map():
     print("Enter the length of all sides")
     lengths  = input("Please enter enter the length of all sides in this
     format: a, b, c ").split(',')
     sides = list(map(int, lengths))
     print(sides[0],sides[1],sides[2])
     return(sides)
 u_input_map()

 # Triangle Perimeter Func.
 # P = a + b + c
 def perim(u_input_map):
     all_sides = (sides[0]+sides[1]+sides[2])
     print(all_sides)
     return sides
 perim(u_input_map)

I just want someone to point me in the right direction. I want to figure this out on my own so I don't need the code.

Indominus
  • 1,228
  • 15
  • 31
techguy1029
  • 743
  • 10
  • 29
  • 3
    Please format your codes first. – Menglong Li Jan 30 '19 at 05:58
  • `perim(u_input_map)` should be `perim(u_input_map())`. At the moment, you are not calling your function, you are passing the function into `perim`, not what the function `u_input_map` returns. – henrycjc Jan 30 '19 at 05:58
  • and this line `all_sides = (sides[0]+sides[1]+sides[2])` should probably be `all_sides = (u_input_map[0] + ...)`, as the variable `sides` is not in the scope of `perim` – henrycjc Jan 30 '19 at 05:59

1 Answers1

2
 #Input Func. using map.
 def u_input_map():
   print("Enter the length of all sides")
   lengths  = input("Please enter enter the length of all sides in this
   format: a, b, c ").split(',')
   sides = list(map(int, lengths))
   print(sides[0],sides[1],sides[2])
   return sides

 # Triangle Perimeter Func.
 # P = a + b + c
 def perim(sides):
   all_sides = (sides[0]+sides[1]+sides[2])
   return all_sides

 old_sides = u_input_map()
 perimeter = perim(old_sides)
Genesis
  • 50
  • 4
taurus05
  • 2,491
  • 15
  • 28
  • 1
    That is generally very bad advice. Please don't do this. It will make your life harder in the long run. – Mad Physicist Jan 30 '19 at 06:04
  • For this specific problem, this is the best thing that can be done.I am aware of the havoc that occours while using global variables, but this is really useful to such small dummy problems. – taurus05 Jan 30 '19 at 06:05
  • Why not assign the return value and pass it to another function? And reuse it later? – Mad Physicist Jan 30 '19 at 06:06
  • Ok @MadPhysicist. I have made the changes. – taurus05 Jan 30 '19 at 06:07
  • I've been coding for a long time in Python, and I've seen very few cases where a problem solved using globals couldn't be solved in a legitimately better way without globals. This is definitely not such a case. – Mad Physicist Jan 30 '19 at 06:08
  • I agree with you! – taurus05 Jan 30 '19 at 06:09
  • You forgot to remove globals in two places, but +1. Doesn't that seem like much nicer code, dummy or otherwise? – Mad Physicist Jan 30 '19 at 06:10
  • 1
    You might want to change the prose to match the code – Mad Physicist Jan 30 '19 at 06:12
  • @taurus05 thanks for this. So the only difference between your code and my code is `sides = u_input_map` and `perim(sides)`. Just so I am understanding this, this first statement allows the `perim` to use sides? – techguy1029 Jan 30 '19 at 06:43
  • 1
    Not excatly. When the first function is executed, i am using the value of sides, by returning it. After the value is returned, i am using it in the another function perim. Don't confuse with the same name sides used, inside the function and outside. It can be any relevant variable name. I have made the changes to eliminate any further confusion. If the answer helps, you can upvote it, so that it can be helpful to others as well. – taurus05 Jan 30 '19 at 06:46