Reputation: 69
So I want to take three inputs from the user and check whether they form a triangle.Now I want my program to check using any three random values of of the given inputs and check whether a + b > c.Here is my code for that :
def check_triangle(a, b, c):
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
def is_triangle(x, y, z):
for i in list_1:
if (list_1[i] <(list_1[i+1] + list_1[i+2])):
print("Yes")
else:
print("No")
check_triangle(a, b, c)
But I am getting no output. What is the error
Upvotes: 1
Views: 1500
Reputation: 483
To begin, since a, b and c are variables input by the user, they do not need to be input into the function as arguments. In fact, because they are not defined before being given as arguments to the function, it is causing the function to raise a NameError
with the message name 'a' is not defined
when it is called. To fix this issue, you can remove a, b and c as arguments to your function in both its definition and its usage.
At this point, the function will run, but even if the user inputs the numbers in the format which your program is expecting (i.e. separated by single spaces - which is not explicitly specified to the user) the portion of your program that evaluates your target condition will not run because it is contained within the function is_triangle(x, y, z) which is not called. This function can be eliminated and your test condition can be evaluated within the main function. Additionally, there is no need to loop through the elements in the list as you can access its elements directly to evaluate your target condition.
Here is the code with these changes made:
# since a, b and c are given by the user, they do not need to be arguments to the function
def check_triangle():
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
# evaluate your test condition directly within the main function. no secondary function is necessary
# as you want to check that c < a + b, access those elements directly by using the index of the list. no loop is necessary
if list_1[2] < list_1[0] + list_1[1]:
print("Yes")
else:
print("No")
# since a, b and c are not defined before running the function, they cause it to raise a NameError if they are given
check_triangle()
This code will run and evaluate your target condition. However, this condition (that c < a + b) does not (to my knowledge) actually mean that something is a triangle. Good luck and happy coding!
Upvotes: 0
Reputation: 17322
you are taking input from the user so you do not need to pass any arguments for your function check_triangle
:
def is_triangle(x, y, z):
if x < y + z:
print("Yes")
else:
print("No")
def check_triangle():
x, y, z = map(int, input('Enter the stick lengths: ').split())
is_triangle(x, y, z)
check_triangle()
or you can simplify you code like:
def is_triangle(x, y, z):
print('Yes' if x < y + z else 'No')
is_triangle(*map(int, input('Enter the stick lengths: ').split()))
Upvotes: 0
Reputation: 186
Your are getting no output, because you defined a function inside a function, but you are calling only the first one. So second one is defined but never executed. What you want to do to execute the second one is to add a function call at the end of the first one, so in your case it would be:
def check_triangle(a, b, c):
a, b, c = [float(i) for i in input('Enter the stick lengths: ').split(' ')]
x, y, z = [int(num) for num in [a, b, c]]
list_1 = [x, y, z]
def is_triangle(x, y, z):
for i in list_1:
if (list_1[i] <(list_1[i+1] + list_1[i+2])):
print("Yes")
else:
print("No")
is_triangle(x,y,z)
Indentation can be messed up, because I'm answering on my phone, sorry for that. Also, from what I can see you will be getting list index out of range error at this line.
if (list_1[i] <(list_1[i+1] + list_1[i+2]))
That is happening because your i is actually an element of your list as defined in the line below, not an index, but you are trying to get an element from the list by it's index with the syntax my_list[index].
for i in list_1
What you want to do instead of the for loop mentioned above is iterate in the range of it's length, meaning iterate over possible indexes in the list, is done like so:
for i in range(len(list_1))
I notice a few other things in your code and a lot of room for improvement, but I hope you can deal with the rest yourself!
Upvotes: 1