Charlie Kimzey
Charlie Kimzey

Reputation: 125

I'm making a triangle class finder and I'm stuck

I am coding a program that lets you type in the three angles or sides of a triangle, and it tells if it's equilateral, isosceles etc. I am not worrying about the rest for now, but I'm stuck on the equilateral part of it. Here is my code:

def idtri():
    print("\nDo you have all three sides, or al three angles?")
    print("(1) Sides")
    print("(2) Angles")
    choice = input()
    if choice == 1:
        print("\nType in the lengths of all of the sides.")
        t1 = input("1: ")
        t2 = input("2: ")
        t3 = input("3: ")
        print("Your triangle is an equalateral triangle.")
        menu()
    elif choice == 2:
        pass

 idtri()

Upvotes: 0

Views: 1670

Answers (2)

Anthony Mooz
Anthony Mooz

Reputation: 3703

You can also use the counter from the Python collections:

from collections import Counter

def idtri():
    cnt = Counter()
    side1 = input('Enter length of first side: ')
    cnt[side1] += 1
    side2 = input('Enter length of second side: ')
    cnt[side2] += 1
    side3 = input('Enter length of third side: ')
    cnt[side3] += 1

    if 3 in cnt.values():
        print('Equilateral Triangle')
    elif 2 in cnt.values():
        print('Isosceles Triangle')
    else:
        print('Scalene Triangle')


if __name__ == "__main__":
    idtri()

Upvotes: 1

ezod
ezod

Reputation: 7421

The first thing to note is that, for identifying the triangle as scalene, isoceles, or equilateral, it doesn't matter whether the three values you have are the angles or the side lengths, the process is:

If all three values are the same, the triangle is equilateral; otherwise, if any two values are the same, the triangle is isoceles; otherwise, the triangle is scalene.

So you can write a simple function to return the type based on the number of equal values provided:

id_triangle = lambda a, b, c: {0: 'scalene', 1: 'isoceles', 3: 'equilateral'}\
                              [(a == b) + (a == c) + (b == c)]

and then call that from your interactive script, like:

print('Your triangle is %s.' % id_triangle(t1, t2, t3))

Upvotes: 1

Related Questions