Name error in Python I don't know how to fix

Using Python, Was doing some stuff when I was doing instructions, weird NameError occurred while doing this.
My code:

suitcase = ["shirt", "shirt", "pants", "pants", "pajamas", "books"]

# Your code below: 
last_two_elements = suitcase[-n:]
print(last_two_elements)

What it gave me:

Traceback (most recent call last):
  File "script.py", line 4, in <module>
    last_two_elements = suitcase[-n:]
NameError: name 'n' is not defined

I don’t know how to fix this help please.

The n is just a placeholder for the value of the index, you can have something like last_two_elements = suitcase[-2:] to get the last two elements and if you must use n then you have to define it earlier like n = 2 before passing n into the slice

Thanks man, You helped me on this error. I didn’t know that [-n:] was just a placeholder.

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