class Triangle:
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z

    def perimeter(self):
        return self.x + self.y + self.z

    def area(self):
        ang = acos(self.y^2 - self.x^2 - self.z^2 / -2 * self.x * self.z)
        h = sin(ang) * self.x 
        return self.z * h / 2

triangle = Triangle(input("Please input one side length of triangle:"),\
                    input("Please input another side length of triangle:"),\
                    input("Please input base of triangle:")):
    if x > y and x > z and x > y + z:
        pass
    elif y > x and y > z and y > x + z:
        pass
    elif z > x and z > y and z > x + y:
        pass
    else:
        print "Not A Valid Triangle"

print triangle.perimeter()
print triangle.area()

The above is my code for creating a triangle's area and perimeter. When I run it says
that the colon after my input statements is invalid syntax. I want to link the input
with the if statement. Any help is appreciated.

Recommended Answers

All 5 Replies

You can add a method to check validity. I also corrected a few other errors

from math import sin, acos

class Triangle:
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z
        if not self.is_valid():
            raise ValueError, "Not A Valid Triangle"

    def perimeter(self):
        return sum(self.sides())

    def area(self):
        x, y, z = self.sides()
        ang = acos((y**2 - x**2 - z**2) / (-2 * x * z)) # the power operator is **, not ^
        h = sin(ang) * x 
        return z * h / 2
    
    def is_valid(self):
        x, y, z = self.sides()
        return all(0 <= a <= b + c for a, b, c in [(x, y, z), (y, z, x), (z, x, y)])
    
    def sides(self):
        return self.x, self.y, self.z

def finput(s):
    return float(raw_input(s)) # in python 2, use raw_input(), not input()

triangle = Triangle(finput("Please input one side length of triangle: "),
                    finput("Please input another side length of triangle: "),
                    finput("Please input base of triangle: "))

Thanks!
I'm new to python so some of the stuff you wrote is new to me,
but thanks for the help.
By the way do you know any good python tutorials.
I've been using http://www.sthurlow.com/python for tutorials
and the writer hasn't mentioned a lot of the things you have.

Thanks!
I'm new to python so some of the stuff you wrote is new to me,
but thanks for the help.
By the way do you know any good python tutorials.
I've been using http://www.sthurlow.com/python for tutorials
and the writer hasn't mentioned a lot of the things you have.

There are many python tutorials. Learn step by step! A well known tutorial is "dive into python" here http://diveintopython.org/ .
Also, when this site works, it links to a whole collection of tutorials: http://python.objectis.net/

By any chance, can you tell me how you learned python?

By any chance, can you tell me how you learned python?

I started with Guido's official tutorial http://docs.python.org/tutorial/ , then I learned on the job by writing my own programs.

By the way, here is a simpler is_valid() method

def is_valid(self):
        a, b, c = sorted(self.sides())
        return c <= a + b # this implies that the 3 numbers are >= 0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.