cjxsam
August 12, 2024, 10:21am
1
Hello everyone, could anyone please help me complete this task pls, I am stuck on the first question
Code: `suitcase = [“shirt”, “shirt”, “pants”, “pants”, “pajamas”, “books”]
Your code below:
last_two_elements = (suitcase[-n:])
print(last_two_elements)t`
Link to Lesson: https://www.codecademy.com/courses/learn-python-3/lessons/use-python-list/exercises/slice-iv
In the hint–it doesn’t ask you to use “n”, it asks what would “n” be?
How do you access the last two items in that list? What are the indicies (counting backwards from 1)? -1? or -2?
Also, you have a stray "t’ " after your print statement.
1 Like
cjxsam
August 15, 2024, 10:58am
3
I am not sure what ‘n’ means
mtf
August 15, 2024, 5:38pm
4
n
is a placeholder variable. It is quite common to let n
be a variable to represent a number, in many cases an integer, such as above.
Eg.
n can be any of 1, 2, 3, ...
The negative sign is the index direction in the above example.
1 Like