samedi 2 mai 2015

Why doesn't my Python program work?

I'm a new programmer, and I decided to pick up Python as my first language.

So I decided to program something where I type in what I want and the program will come up with something. This is a work in progress, and is constantly being improved. However, I ran into an error with a section of my program. I decided to program something that could find the side of a right triangle, given the other sides. So let me show it to you.

tri = raw_input("What side do you need? ")

if tri == "a":
    def a(b, c):

        b = float(b)
        c = float(c)

        if c-b >= 0:
            a = (c-b)*1/2

            print "a is equal to %s" % a

        else:
            print "This triangle doesn't exist!" 

    print a(raw_input("Ok, what is b? "), raw_input("And what is c? "))

elif tri == "b":
    def b(a, c):

        a = float(a)
        c = float(c)

        if c-a >= 0:
            b = (c-a)*1/2

            print "b is equal to %s" % b

        else:
            print "This triangle doesn't exist!" 

    print a(raw_input("Ok, what is a? ", raw_input("And what is c? "))

elif tri == "c":
    def c(a, b):

        a = float(a)
        b = float(b)

        if a+b > 0:
            c = (a+b)*1/2

            print "c is equal to %s" % c

        else:
            print "This triangle doesn't exist!" 

    print c(raw_input("Ok, what is a? ", raw_input("And what is b? "))

 else:
    print "You must choose a valid side of a triangle!"

So apparently everything is fine, but the the second elif (the one to find c, given a and b) turns up a syntax error. I have no idea why it is wrong, since it appears to be consist with the if elif else format. Can someone help me find the problem and fix it?

Aucun commentaire:

Enregistrer un commentaire