2

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

4 回答 4

5

这一行:

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

缺少一个结束括号。编程需要注意很多细节...

于 2012-06-23T01:58:57.903 回答
3
 s = int(input('What is the side length')

你错过了一个结束)

事实上,我注意到您在 中的其他input语句rhombuspentagon并且trapezoid 有类似的问题,您可能复制了代码:)

您可能想要使用一个可以帮助您匹配左括号和右括号的编辑器。这将有助于避免此类错误。

于 2012-06-23T01:59:39.570 回答
2

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

于 2012-06-23T01:59:36.710 回答
0

这些行中缺少右括号:

在函数五边形()

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

在函数菱形()

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

在函数梯形()

baseOne = int(input('What is the first base?')
baseTwo = int(input('What is the second base?')
h = int(input('What is the height')
于 2012-06-23T02:54:06.300 回答