matacusa
matacusa

Reputation: 9

Syntax Error in simple Python code

I found an exercise online to make a specific program, kinda got lost writing, and forgot to test for far too long. Now the program is spitting out an error - and a very strange one, at that.

I didn't know how much of it to post, but I'm putting it all out here just in case.

import math
print "Hey. Do you want to activate the hypothenuse program, or the diagonals one?"
print "[1] Hypothenuse"
print "[2] Diagonals"
program_chosen = raw_input()

if program_chosen == "1":

    print """
        Hello. This is a simple math program to determine the length of the hypothenuse of
        a right triangle given the other two sides. The results will be rounded to three decimal
        places, to avoid confusion.
        """
    # Once the other problems have been dealt with, implement a "choose digits rounded" option.
    side1 = int(raw_input("Please enter the length of any of the non-hypothenuse sides of the triangle. "))
    side2 = int(raw_input("Now, enter the length of the other side. "))
    pythagoras = math.sqrt((side1**2)+(side2**2))
    # Need to define another variable to choose the number of rounded-to digits.
    print "The length of the hypothenuse is , approximately, " + str((round(pythagoras, 3))) + "."  

elif program_chosen == "2":
    print """
        Very well. The following program is one which, given an n-sided polygon, finds the number
        of diagonals formed by its vertexes.
        """ 
    n_of_sides = int(raw_input("To start, please enter the number of sides of the polygon. "))
    if n_of_sides < 3
        print "That isn't a polygon, silly! Please try again."
        # Need to find a way to repeat the prompt until a valid number of sides is inputted.
        # Probably a While loop, but haven't learned how to use one effectively yet.
        # Apparently a goto is not good programming form. :(
        exit()
    else
        n_of_diagonals = (n_of_sides*(n_of_sides-3))/2
        print " A %d sided polygon has %d diagonals." % (n_of_sides, n_of_diagonals)

else:
    print "That is not a valid option. Please try again."
    # Here, too, need to implement a "try again" option.
    exit()

The thing is, when running it, PowerShell told me the line 7 if statement breaks (SyntaxError: invalid syntax) because I'm using a single equals sign (which I found was a variable setter and not a comparison), but even after changing it to a double equals one the error continued appearing. So I tried it again, just to be sure, on afterhours' code tester, and a "ParseError: bad input on line 27" error appeared.

So I'm really confused. Please help?

Upvotes: -2

Views: 168

Answers (1)

Stidgeon
Stidgeon

Reputation: 2723

Specifically, you missed two colons:

    if n_of_sides < 3: #######
        print "That isn't a polygon, silly! Please try again."
        # Need to find a way to repeat the prompt until a valid number of sides is inputted.
        # Probably a While loop, but haven't learned how to use one effectively yet.
        # Apparently a goto is not good programming form. :(
        exit()
    else:  #####

Upvotes: 2

Related Questions