简单Python代码中的语法错误

时间:2016-01-07 17:13:31

标签: python powershell syntax-error

我在网上发现了一个特定程序的练习,有点迷失了写作,忘了测试太久了。现在程序正在吐出一个错误 - 而且是一个非常奇怪的错误。

我不知道要发布多少内容,但为了以防万一,我将它全部放在这里。

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()

问题是,在运行它时,PowerShell告诉我第7行if语句中断(SyntaxError:无效语法),因为我使用的是单个等号(我发现它是一个变量setter而不是比较),但即使将其更改为双等于1之后,错误仍继续出现。所以我再次尝试了,只是为了确定,在下班后的代码测试器上,出现了“ParseError:第27行的错误输入”错误。

所以我真的很困惑。请帮帮忙?

1 个答案:

答案 0 :(得分:2)

具体来说,你错过了两个冒号:

    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:  #####