My code broke i do not know why

how do i make it print all items in form using indexing:


Repl Link

list = ["apple","cherry"]
x = list[0:1]
print(x)

the out come was apple and not apple cherry

1 Like

it continues to make me mad

Hi @TheOtherGuy thanks for your query.

I’ve moved it into Code Help → Python for future reference.

The reason your output is cherry is because Python needs the end position of the list to be one after (e.g. the length of the list in this instance is 2). It’s exactly the same as with fixed loops.

For example:

list = ["apple","cherry"]
x = list[0:2]
print(x)
1 Like

thanks you for you comment

Does this work for you?

list = ["apple","cherry"]
for item in list:
  print(item)

Heya, @TheOtherGuy! I think you need to do this:

list = ["apple", "cherry"]
x = list[0:len(list)]
print(x)

Hi @RixTheTyrunt that’s true but essentially the same as the earlier answer.

1 Like

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