简体   繁体   中英

I've got an “invalid syntax” error with this code in python. Help me find a remedy?

In the def(pentagon): chunk I've named a variable " first ". However, this causes an "invalid syntax" error. What's wrong? I've tried naming it other things, ranging from single letters to combinations of lower/capital letters like "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() 

This line:

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

is missing a closing paren. Programming requires paying attention to many many details...

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

You are missing a closing )

In fact I noticed that your other input statements in rhombus , pentagon and trapezoid have a similar problem, you probably copied code :)

You might want to use an editor that will help you match open and closing parenthesis. It will help avoid these sort of errors.

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

Closing parentheses is missing in these lines:

in function pentagon()

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

in function rhombus()

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

in function trapezoid()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM