Why is this happening?

So basically, I use Python, a popular programming language. There’s this one problem that I just don’t understand why happening. I’m learning " Accessing 2D lists in Python" which my code was like:

class_name_test = [["Jenny", 90] ["Alexus", 85.5 ["Sam", 83] ["Ellie",101.5]]
print(class_name_test)

What it gave me:

File "script.py", line 2
    print(class_name_test)
        ^
SyntaxError: invalid syntax

What’s happening? Please give me advice.

Your array isn’t written with correct syntax

Thanks but, the error is in the print statement not the array.

there are missing commas between some of the inner lists.

Thanks but the error is in the print statement.

you missed commas between the inner lists’ elements. Each inner list should be separated by a comma to distinguish one list from another within the main list

The errors in the array are confusing Python. You need to fix the array.

But then why does it tell me there is an error in the print statement if your telling me the array is confusing Python?

I would not get too hung up on this. You have an error in a two line program and the print statement is very simple. You can see there is no syntax error there.

There is a concept called “traceback” where you might get an error in a statement but it’s caused by a previous statement and you need to “trace back” to what caused the error. Sometimes you will get a more detailed traceback error that follows the error to it’s source.

Not sure what you are using to code, but I put this program into Google Collab and it shows this error:

File "<ipython-input-1-875ad81545af>", line 1
    class_name_test = [["Jenny", 90] ["Alexus", 85.5 ["Sam", 83] ["Ellie",101.5]]
                      ^
SyntaxError: '[' was never closed

It might be more helpful. In any case if you look closely at your array, you are clearly missing a bracket. Make it easier to look at:

class_name_test = [
    ["Jenny", 90] 
    ["Alexus", 85.5 
    ["Sam", 83] 
    ["Ellie",101.5]
]

Does this help?

2 Likes

Thanks man! It really helped a lot but the error has got changed into a type error after fixing the code.
My code:

class_name_test = [
  ["Jenny", 90] 
  ["Alexus", 85.5] 
  ["Sam", 83] 
  ["Ellie",101.5]
  ]

I fixed the error in Alexus list then,
the terminal:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    ["Alexus", 85.5] 
TypeError: list indices must be integers or slices, not tuple

If you can’t help me out it is fine.

1 Like

What you have here is a nested list. A list within a list. All list elements have a comma between them. ["Jenny", 90] Here is a list with two elements. It’s contained within a list of lists. Now that “outside list” I do not see commas

https://www.learnbyexample.org/python-nested-list/

For what you are building though, a dictionary might be better

https://www.w3schools.com/python/python_dictionaries.asp

1 Like

Thanks man, you helped me get through this. And thanks to the others who helped me too.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.