初学者的未知语法错误

时间:2013-07-19 23:10:14

标签: python syntax

我遇到一个奇怪的问题,其中一个'if语句'后面的冒号(用注释标记)被标记为语法错误。我对编程很陌生,真的不明白为什么!

#This program calculates different shapes, their lengths/area
#pythag and 1/2bh for triangles

#GLOBAL CODE
nSquareSide = float
nSquareArea = float
nShapeBase = float
nShapeHeight = float
nShapeArea = float
sChooseShape = str
sChooseLA = str #do you want to fins length or area?

#FUNCTIONS
#arranged in 'shapeCalcA' for area (A) or 'shapeCalcL' for length (L)

def squareCalcA(nSquareSide):
    return float(nSquareSide ** 2)
def sqareCalcL(nSquareArea):
    return float(nShadeArea ** 0.5)
def triCalcA(nShapeBase, nShapeHeight):
    return float((0.5 * nShapeBase) * nShapeHeight)
#pythag goes here (work in progress)


#MAIN

while 1==1:
    sChooseShape = str(input("What shape would you like to find the area/length of? "))

    if sChooseShape in ['Square','square','sqr']:
        sChooseLA = str(input("You have chosen square./nWould you like to find area or length? ")
        if sChooseLA in ['area','a','A','Area']: #ONLY SYNTAX ERROR ON THIS COLON
            nSqaureSide = float(input("Enter side length of square: "))
            nSquareArea = squareCalcA(nSqaureSide)
            print("The area of a square with a side length of " + str(nSqaureSide) + " is " + str(nSqaureArea)
            break
        elif sChooseLA in ['length','Length','L','l']:
            nSqaureArea = float(input("What is the area of the square? "))
            nSqaureSide = sqaureCalcL(nSqaureArea)
            break
        else:
        print("You did not enter a valid input, please restart...")

2 个答案:

答案 0 :(得分:2)

)

之后你错过了sChooseLA = str(input("You have chosen square./nWould you like to find area or length? ")

答案 1 :(得分:2)

您需要在print

之后缩进else语句
elif sChooseLA in ['length','Length','L','l']:
    nSquareArea = float(input("What is the area of the square? "))
    nSquareSide = sqaureCalcL(nSqaureArea)
    break
else:
    print("You did not enter a valid input, please restart...")

除了语法错误之外,还有其他几个问题(拼写错误的变量名称,seth指出的缺少括号等)。 您可能希望阅读Python教程,例如version 2.xversion 3.x。例如,您不需要在Python中预先声明变量,因为您似乎尝试使用这些行:

# None of these are needed. These do nothing but define aliases for various type
# constructors that will be overwritten later anyway.
nSquareSide = float
nSquareArea = float
nShapeBase = float
nShapeHeight = float
nShapeArea = float
sChooseShape = str
sChooseLA = str #do you want to fins length or area?