0

I am making a program that determines if a triangle is obtuse(O),acute(A) or right(R)

Enter in the length of angles
>>> 3 8 9
TypeError: pythag() takes exactly 3 arguments (1 given)

I understand why I'm getting the error but what I'm trying to do is somehow loop the values of the list into the function and then call the function.

input = raw_input("Enter in the length of angles\n>>> ") #>>> 3 8 9
lst = input.split() #splits the input into a list by whitespace
for i in lst: #converts the values in the list to integers
    int(i)

def pythag(a,b,c): #function to perform calculation
    if a^2 + b^2 != c^2:
        if c^2 > a^2 or b^2:
            return "A"
        else:
            return "O"
    else:
        return "R"

pythag(lst)

Any suggestions?

Thanks in advance.

avbirm
  • 105
  • 1
  • 1
  • 9
  • 1
    `pythag(*lst)` will do the job. Related: http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters – Łukasz Rogalski Apr 04 '16 at 14:53

1 Answers1

3

First of all, int(i) does nothing to the original list, so it still contains strings. Second, which is where your exception happens, is that you are still passing a single argument to the function.

lst = [int(i) for i in lst]  # convert the input
pythag(*lst)  # same as pythag(lst[0], lst[1], ...)
bereal
  • 32,519
  • 6
  • 58
  • 104
  • Could you please elaborate on why my original for loop doesn't convert the items of the list into integers? – avbirm Apr 05 '16 at 14:57
  • 1
    @avbirm `int(i)` converts the passed argument to an integer without modifying its value (which is in our case a string and as such is immutable anyway). Even if you wrote `i = int(i)` in the same loop, that would have no effect on the original list, because it would just rebind name `i` to the new value. – bereal Apr 05 '16 at 15:05
  • Got it, so basically the difference is that your converting the values to int's with the "lst =" rather than just writing a for loop that modify's only i. – avbirm Apr 05 '16 at 15:21
  • 1
    @avbirm depends on what you mean by modifying `i`, because it may mean changing the state of the object referred to by name `i`, or sometimes re-binding the name `i` to the new value. Neither actually happens in the original code. – bereal Apr 05 '16 at 15:26