[英]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.