2013-07-19 152 views
-2

我遇到了一個奇怪的問題,其中一個冒號(標記爲音符)後面的'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

你缺少)sChooseLA = str(input("You have chosen square./nWould you like to find area or length? ")

+0

是的。這也是我注意到的問題。 – erewok

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? 
+0

一位程序員朋友告訴我,我所做的原始聲明使程序更易於閱讀。那麼這是錯誤的? –

+0

我懷疑你的程序員朋友不會編寫太多的Python代碼。它本身並不是錯誤的,但它並不真正起任何作用。如果真的有必要,可以用解釋者不需要處理的註釋來完成同樣的事情。 – chepner