简体   繁体   English

尝试调用python函数时不断出现语法错误

[英]Keep getting syntax error when trying to call python function

Can you guys help me out?你们能帮我吗? I've been stuck trying to figure out what I'm doing wrong when calling this function.我一直在试图找出调用此函数时我做错了什么。 I have previous examples that, at least to my eyes look exactly the same and work just fine.我之前有一些例子,至少在我看来,它看起来完全一样,而且工作得很好。 VSCode claims the syntax error is in the call. VSCode 声称语法错误在调用中。 I'm sure it's a simple fix but it's driving me mad, Thanks.我确定这是一个简单的修复,但它让我发疯,谢谢。

lst = [4, 6, 1, 3, 5, 7, 25]

def stars(myList):
    for i in range(0, len(myList - 1)):
        print ("*" * (myList[i])
stars(lst)

There are two errors in the code.代码中有两个错误。

One is the missing parentheses after print ("*" * (myList[i]) that will give bellow error一个是print ("*" * (myList[i]))后缺少的括号,会出现以下错误

SyntaxError: invalid syntax语法错误:无效语法

So you need to add parentheses as print ("*" * (myList[i]))所以需要加上括号为print("*" * (myList[i]))

Second is the wrong condition in range.其次是范围内的错误条件。

len(myList - 1) will below error as you cannot subtract from the list directly. len(myList - 1)将低于错误,因为您不能直接从列表中减去。

First, you need to get the length by len(myList) then subtract as len(myList) - 1首先,您需要通过 len(myList) 获得长度,然后减去len(myList) - 1

TypeError: unsupported operand type(s) for -: 'list' and 'int'类型错误:不支持 - 的操作数类型:'list' 和 'int'

Below is the correct code that should work下面是应该工作的正确代码

lst = [4, 6, 1, 3, 5, 7, 25]

def stars(myList):
    for i in range(0, len(myList) - 1):
        print ("*" * (myList[i]))
stars(lst)

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

相关问题 我正在尝试创建一个 cal_average 函数,但是我不断收到 python 无法分配给函数调用的语法错误 - I am trying to create a cal_average function however I keep getting the syntax error that python cannot assign to function call 尝试调用函数时Python IDLE中的语法错误 - Syntax error in Python IDLE when trying to call a function Python:尝试调用函数时出现未定义变量错误? - Python: Getting an undefine variable error when trying to call a function? 我是python和pynag的新手,在尝试将pynag命令放入我的python脚本时,我不断收到语法错误 - I'm new to python and pynag and I keep getting a syntax error when trying to put a pynag command into my python script 尝试调用python函数时出现Cronjob错误 - Cronjob error when trying to call python function 尝试在函数中添加两个全局变量时出现语法错误 - Getting a syntax error when trying to add two global variables in a function 尝试在 Python 中执行此 SQL 查询时出现语法错误 - Getting a syntax error when trying to execute this SQL query in Python 我在python中不断收到语法错误 - I keep getting an syntax error in python 尝试从 Python 中的另一个函数调用函数时出错 - Error when trying to call a function from another function in Python 试图将PHP转换为python并获得语法错误 - Trying to convert php to python and getting a syntax error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM