Is triangle a function in python?

Dec-13-2019, 09:57 AM (This post was last modified: Dec-13-2019, 02:28 PM by ichabod801.)

I'm pretty much having problems getting this program to run and i'm not sure whats wrong with my code in order to make sure everything is inputted correctly, I appreciate the assistance guys. Here is the assignment below:

For this assignment, you need to create a function that implements the Triangle Classification Algorithm. The algorithm has a lot of steps in it, but each step is simple. We have covered enough material to implement this.
The Triangle Classification Algorithm accepts three integer lengths and it returns a 1, 2, 3, or 4. The three lengths represent possible lengths to the sides of a triangle. Returning a 1 means that the lengths would form a scalene triangle. Returning a 2 means that the sides would form an isosceles triangle. Returning a 3 means that the sides would form an equilateral triangle. Returning a 4 means that the sides cannot form a triangle. For example, sides 1, 1, and 10 cannot form a triangle.
Below you will find a control flow graph of the algorithm. The brackets in the graph [ and ] are just there as a means of referencing each condition. Also, the || symbol is or. The top oval says, "Read i, j, k". These variables should be passed in as parameters to your function. The second oval is equivalent to
if i <= 0 or j <= 0 or k <= 0
Everything else on the chart should be easy to understand, but ask questions if needed.
Write a function for the implementation of the Triangle Classification Algorithm. Also, write a program that uses the function. This program should ask the user to enter the three lengths, and it returns the result of calling the function.
Triangle Classification Algorithm

def triangle():
    if i<=0 or j <=0 or k<=0:
        print ("4 does not form a triangle")
    return (4)
i = float(input("i:"))
j = float(input("j:"))
k = float(input("k:"))
    


if i> 0 and j >0 and k>0:
    triangle = 0
    if i == j:
        triangle = tri + 1
    if i == k:
        triangle = tri + 2
    if j == k:
        triangle = tri + 3
      

if triangle == 0:
    if (i + j <= k) or (j + k <= i) or (i + k <= j):
        tri = 4
        print (triangle, "does not form a triangle")
return triangle
elif:
        tri = 1
        print (triangle, " a scalene triangle")
        return triangle

if tri != 0:
    if tri > 3:
        tri = 3
        print (triangle, " an equilateral triangle")
        return triangle
    elif:
        if (triangle == 1) and (i + j >k):
            triangle = 2
            print (triangle, " an isoceles triangle")
            return triangle
        elif (triangle == 2) and (i + k >j):
            tri = 2
            print (tri, "an isoceles triangle")
            return tri
        elif (tri == 3) and (j + k > i):
            triangle = 2
            print (triangle, "an isoceles triangle")
            return triangle
        elif:
            triangle = 4
            print (triangle, "does not form a triangle")
            return triangle

Posts: 4,231

Threads: 97

Joined: Sep 2016

Reputation: 273

First, return cannot be used outside of a function, so line 25 needs to go.

Second, elif needs a condition, just like if. An if chain might look like this:

if a == 1:
    z = x + y
elif a == 2:
    z = x * y
else:
    z = x - y

Python checks the condition for the if statement. If it's true, it executes the code indented under the if and then skips past any elif's or else's. If the condition for the 'if' is false, it checks the next 'elif' condition. It keeps checking elif's until one of them is true, then it executes that elif's code, and skips to the end. If none of the elif conditions are met (or if there were no elifs), the code under the else clause is executed (if there is an else clause). The else statement is the only one of the three that does not have a condition. If and elif must have a condition, the else must not have a condition. The condition for the else statement is implicit: none of the other conditions were true.

Note that another if statement stops that chain and starts a whole new one, completely independent of the conditions of the earlier one.

Posts: 155

Threads: 26

Joined: Jan 2019

Reputation: 8

Your indentation is way off

When you define a function all of the code in that function must be indented. The first line that unindents ends the function's block of code. Is all of this code supposed to be in your function?

You also missed the requirement of collecting the variables and passing them into the function as parameters.

Here is an example of a function that takes a parameter:

def plus_one(num_input): # Parameters go in parentheses 
    incremented = num_input + 1
    # Any other code for this function must be indented like this
    # As ichabod801 said it must all come before the return statement as well
    return incremented

# Since this code is not indented it is not part of the function
age = int(input("What is your age: ")) # Collect your variable
next_years_age = plus_one(age) # Send variable into the function and store the result
print("our age next year will be", next_years_age) # Use the result produced by the function

Try again and post your results

What are functions in Python?

In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes the code reusable.

How do you code a triangle in Python?

How to Draw a Triangle in Python Turtle.
Draw a line with pen - forward() command..
Move without drawing - penup(), pendown() commands..
Turn the pen to an angle - left(), right() commands..

How do you call a function in Python?

Function Calling in Python.
def function_name():.
Statement1..
function_name() # directly call the function..
# calling function using built-in function..
def function_name():.
str = function_name('john') # assign the function to call the function..
print(str) # print the statement..

How do you check if a triangle is right

def right_angled(a, b, c): if (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b) : return "The triangle is right-angled." else: return "The triangle is not right-angled."