我有一个"无效的语法" python中此代码出错。帮我找一个补救措施?

时间:2012-06-23 01:57:14

标签: python

def(pentagon):块中,我将变量命名为“first”。但是,这会导致“无效语法”错误。怎么了?我已经尝试过命名其他东西,从单个字母到低级/大写字母组合,如“preArea”。

def display():
    print('This program will tell you the area some shapes')
    print('You can choose between...')
    print('1. rectangle    2. triangle')
    print('3. circle       4. pentagon')
    print('5. rhombus      6. trapezoid')

def shape():
    shape = int(input('What shape do you choose?'))
    if shape == 1: rectangle()
    elif shape == 2: triangle()
    elif shape == 3: circle()
    elif shape == 4: pentagon()
    elif shape == 5: rhombus()
    elif shape == 6: trapezoid()
    else:
        print('ERROR: select 1 2 3 4 5 or 6')
        shape()


def rectangle():
    l = int(input('What is the length?'))
    w = int(input('What is the width?'))
    areaR=l*w
    print('The area is...')
    print(areaR)


def triangle():
    b = int(input('What is the base?'))
    h = int(input('What is the height?'))
    first=b*h
    areaT=.5*first
    print('The area is...')
    print(areaT)


def circle():
    r = int(input('What is the radius?'))
    preCircle = r**2
    areaC = 3.14*preCircle
    print('The area is...')
    print(areaC)


def pentagon():
    s = int(input('What is the side length')
    first = s**2
    areaP = 1.72*first
    print('The area is...')
    print(areaP)


def rhombus():
    b = int(input('What is the base?')
    h = int(input('What is the height?')
    areaR = b*h
    print('The area is...')
    print(areaR)


def trapezoid():
    baseOne = int(input('What is the first base?')
    baseTwo = int(input('What is the second base?')
    h = int(input('What is the height')
    first = baseOne*baseTwo
    second = first/2
    areaT = second*h
    print('The area is...')
    print(areaT)


if __name__=="__main__":
    display() 
    shape() 

4 个答案:

答案 0 :(得分:5)

这一行:

s = int(input('What is the side length')

错过了闭幕式。编程需要关注许多细节......

答案 1 :(得分:3)

 s = int(input('What is the side length')

您错过了结束)

事实上,我注意到inputrhombuspentagon中的其他trapezoid语句存在类似问题,您可能会复制代码:)

您可能希望使用能够帮助您匹配开括号和右括号的编辑器。它有助于避免这些错误。

答案 2 :(得分:2)

缺少右括号:s = int(input('What is the side length'))

答案 3 :(得分:0)

这些行中缺少结束括号:

在函数pentagon()

s = int(input('What is the side length')
函数rhombus()

中的

b = int(input('What is the base?')
h = int(input('What is the height?')

在函数trapezoid()

baseOne = int(input('What is the first base?')
baseTwo = int(input('What is the second base?')
h = int(input('What is the height')