简单 Python 代码中的语法错误
Syntax Error in simple Python code
我在网上找到了一个练习来制作一个特定的程序,有点写不下去了,忘记测试太久了。现在程序吐出一个错误 - 而且是一个非常奇怪的错误。
我不知道有多少要 post,但我把它全部放在这里以防万一。
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 而不是比较),但即使将其更改为双精度等于一,错误仍继续出现。所以我再次尝试,只是为了确定,在下班后的代码测试器上,出现了 "ParseError: bad input on line 27" 错误。
所以我真的很困惑。请帮忙?
具体来说,您漏掉了两个冒号:
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: #####
我在网上找到了一个练习来制作一个特定的程序,有点写不下去了,忘记测试太久了。现在程序吐出一个错误 - 而且是一个非常奇怪的错误。
我不知道有多少要 post,但我把它全部放在这里以防万一。
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 而不是比较),但即使将其更改为双精度等于一,错误仍继续出现。所以我再次尝试,只是为了确定,在下班后的代码测试器上,出现了 "ParseError: bad input on line 27" 错误。
所以我真的很困惑。请帮忙?
具体来说,您漏掉了两个冒号:
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: #####