What's wrong with this code?

I’m trying to learn how do lists work and was coding in order to figure out my own mistakes .I got an error,here’s my code:

xcz= ['w','s']
xcz.append("aaz")
xcz+= [51,"sry",False,[41.32,"eaf"]]
print(xcz)
print(len(xcz))
if not xcz[0] == "w" and xcz[-3:] =="sry",False,[41.32,"eaf"]:
    print("yayyy")
elif not xcz[-4] ==51 or len(xcz)!= 8:
    print("woahh")
else:
    print("awwww")

The error says:

‘:’ expected :6

i guess it says “:” is missing somewhere but i couldn’t find the problem.

Have you found the exact line where the issue is found, if not consider running line by line until you do. Since you mentioned hunting your own mistakes then it’s probably worth splitting up the problematic line until you can find the issue, some of these lines are quite busy and might mask the issue at first glance (as well as confuse the parser enough so it focuses on the wrong thing). Just make sure each subsequent expression you use can evaluate without issue and then start piecing it all back together until you find where it breaks again :laughing:.

Silly perhaps but trial and error can often work when all else fails. After all, that’s what a lot of real testing does for you.

1 Like

doesn’t the error message which says

‘:’ expected :6

mean that the problem is ‘:’ is missing on line 6?
what i meant by saying “somewhere”, i meant somewhere on line 6 but i couldn’t find it.

but i found the issue which is on line 6 but not about adding ‘:’ or not lol.
i’m little bit confused tbh.

as you said i tried to run each expression one by one and i added extra square brackets to
xcz[-3:] =="sry",False,[41.32,"eaf"]

and change it into:

xcz[-3:] ==["sry",False,[41.32,"eaf"]]

code runs and output is “woahh”.

and if i change
len(xcz)!= 8:
to
len(xcz)!= 7:

output becomes “awwww”.

thanks. :))

1 Like

I think the issue is using the , is an ambiguous setting. Python will sometimes create tuple when separating objects by commas on a single line-

x = 1, 2, 3,
print(f'{type(x) = } and {x = }')

Out: type(x) = <class 'tuple'> and x = (1, 2, 3)

But this only occurs in non-ambiguous settings like the single line example above (you might find them used this way in the return statement for example).

Since it’s not creating a tuple I assume the parser is looking for the end of if statement (the : colon) but finds something else and raises the exception.

A roundabout fix would be changing the operator precedence with partentheses, e.g.

# evaluate that part of the expression earlier...
if (xcz[-3:] == "sry",False,[41.32,"eaf"]):
    dosomething

# evaluate the comma separation earlier making a tuple...
if xcz[-3:] == ("sry",False,[41.32,"eaf"]):
    dosomething

What Python version/IDE is that btw? Just tried running it on my own (3.9) and the error is significantly more helpful-

  File "/path/to/test.py", line 6
    if not xcz[0] == "w" and xcz[-3:] =="sry",False,[41.32,"eaf"]:
                                             ^
SyntaxError: invalid syntax

That’s showing the line and a significantly clearer warning.

i use Python 3.10 and Pycharm
i also get the exact same error message

line 6
    if not xcz[0] == "w" and xcz[-3:] =="sry",False,[41.32,"eaf"]:
                                             ^
SyntaxError: invalid syntax

what i sent is what i saw when i clicked the error sign at the top right corner beside the yelllow sign shows weak warnings.

‘:’ expected :6

i prefer to look there to try to fix my mistakes,is that much harder way to do it than looking the run tool window?

i’m a newbie so there’s a long road ahead to approach everything in a smarter way :))

1 Like

I guess the small yellow warning is from your current code editor/IDE? By all means use it as I guess they’re supposed to be quick and convenient messages.

If however it’s not providing enogh information then yes the standard traceback is probably much more information rich which would make it easier to find and deal with the bug.

1 Like