Python Forum
Triangle function program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Triangle function program
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,847 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,363 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,929 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,080 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Print triangle using while loop tuxandrew 3 4,903 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,586 May-19-2019, 06:04 PM
Last Post: heiner55
  Draw isosceles triangle fen1c5 4 12,821 Jul-07-2018, 10:20 AM
Last Post: fen1c5
  Pascal's triangle nvakada 5 4,601 May-01-2018, 02:44 AM
Last Post: Skaperen
  Object oriented area of a triangle xterakojede 2 8,926 Apr-20-2018, 01:42 PM
Last Post: xterakojede
  Turtle drawing Right Triangle Zatoichi 3 5,713 Feb-26-2018, 12:24 AM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020