简体   繁体   English

我在python中使用此代码时出现“无效语法”错误。 帮我找一个补救措施?

[英]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 ". def(pentagon): chunk中我将变量命名为“ 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". 我已经尝试过命名其他东西,从单个字母到低级/大写字母组合,如“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 :) 事实上我注意到你在rhombuspentagontrapezoid中的其他input语句有类似的问题,你可能复制了代码:)

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() 在函数trapezoid()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 代码中用于查找素因子的语法错误。 如果有人可以帮助我,我将不胜感激 - Syntax error in python code to find Prime-factors. Id appreciate if someone could help me 请帮我找到代码中的语法错误 - Please help me find the syntax error in my code 在安装Wagtail for Python时遇到问题。 收到无效语法错误 - Having trouble with installing Wagtail for Python. Getting error of invalid syntax 有人可以帮我解决为什么会发生这种情况吗? Python无效语法错误 - Could someone please help me with why is this happening? Python invalid syntax error Python 语法错误:我的代码中的语法无效 - Python Syntax Error : Invalid Syntax in My Code 我在python中以OOP方式创建了一个简单的计算器。 我收到一条错误消息,提示我的变量未定义 - I've created a simple calculator in OOP way in python. I get an error message, that my variables are not defined 从Internet复制的Python Code I中的语法错误无效 - Invalid Syntax error in Python Code I copied from the Internet 为什么在这段代码(Python)中出现无效的语法错误? - Why do I get an invalid syntax error in this piece of code, Python? 我正在用 python 做一个语音助手,遇到了一个错误,你能帮我解决这个问题吗? - I am doing a voice Assistant with python and got an error can you help me with this? python中的语法错误。 带有=符号的错误 - Syntax error in python. Error with an = sign
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM