Since the title, I hope, will suggest, this is an example for the specified book. I am still new to programming and am having difficulty debugging. With that said, any criticism is welcome, especially if it shows a more efficient way of coding; just keep in mind that I'm still a beginner, so I have a good chance that I don’t know what you mean if you drop me a new built-in function or something like that.
So, the point of this exercise is to write a function that gives three arguments to determine if these three arguments form a triangle. Here is my code:
def is_triangle(a,b,c): num_list = [a,b,c] biggest = max(num_list) other_two = num_list.remove(biggest) sum_of_two = sum(other_two) if sum_of_two > biggest: print 'Congrats, %d, %d, and %d form a triangle!' % (a,b,c) elif sum_of_two == biggest: print 'That forms a degenerate triangle!' else: print 'That does\'t make any sort triangle... >:[' def sides(): side1 = raw_input('Please input side numero Juan: ') side2 = raw_input('Now side two: ') side3 = raw_input('...aaaannnd three: ') import time time.sleep(1) print 'Thanks >:]' side1 = int(side1) side2 = int(side2) side3 = int(side3) is_triangle(side1,side2,side3) sides()
However, when I run it, I get the following:
Traceback (most recent call last): File "A:/Python/is_triangle.py", line 27, in <module> sides() File "A:/Python/is_triangle.py", line 25, in sides is_triangle(side1,side2,side3) File "A:/Python/is_triangle.py", line 5, in is_triangle sum_of_two = sum(other_two) TypeError: 'NoneType' object is not iterable
My guess is the string sum_of_two, but I don’t know what is wrong with it. Can someone help me debug this?
I spend a good hour rewriting it using the built-in function (differently, with a bouquet of or ). But it looked awful, and I'd rather learn to write like that.