-1

I'm trying to get my list to slice the first 3 items off. I interactively ask for 7 names and output the original list first. When I ask the code to slice the list, it seems to have no knowledge of names inside the list.

#Example names for testing:
#Liam Noah Eli Logan Mason James Aiden
names=[input("Please enter 7 names: ")]
for a in names:
    print(a)
for b in names:
    print(b[3:])

  • Why are you looping yet printing the list itself? – SuperStormer Oct 08 '20 at 16:15
  • Let me see what I can see that'll help! Thanks! – Nicholas Fedon Oct 08 '20 at 16:22
  • What do you mean by "names"? What names should your code have knowledge of? – Code-Apprentice Oct 08 '20 at 16:23
  • To slice of the first 3 numbers, just do `print(numbers[3:])`. There is no need for a loop unless you want to print them in a different format. But then you should do a loop like `for a in numbers[3:]: print(a)` Note that you do `print(a)` to print the element, not `print(numbers)` which prints the entire list. – Code-Apprentice Oct 08 '20 at 16:24
  • I misread what my instructor was wanting. She wants names inputted instead of numbers – Nicholas Fedon Oct 08 '20 at 16:25
  • @NicholasFedon Sounds like you need to modify your code to do this. Are the names single words? Should they be entered all on a single line separated by spaces? Or should they each be on a separate line? When you make these modifications and if you need help, feel free to post a new question. – Code-Apprentice Oct 08 '20 at 16:27
  • 1
    @Tomerikoo At this point, posting a new question would be better. – Code-Apprentice Oct 08 '20 at 16:28
  • @Tomerikoo Agreed. I'll finish closing this question and the OP can post a new one when they are ready. – Code-Apprentice Oct 08 '20 at 16:36
  • I just uploaded my new code, apologies for my confusion. – Nicholas Fedon Oct 08 '20 at 16:40
  • Your question now is a duplicate: https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list. You say *it seems to have no knowledge of names inside the list*. Of course it doesn't. Your list has just one string which is the input. You need to `split()` it in order to get a list of words – Tomerikoo Oct 08 '20 at 16:42
  • It's weird because in the previous numbers version you did use `split()` so it seems like you know about it. Why didn't you use it here as well to separate the words? `names = input("Please enter 7 names: ").split()` – Tomerikoo Oct 08 '20 at 16:45
  • I utilized it in the numbers version unaware I was creating the incorrect requested program. I was not aware I still required to use split() – Nicholas Fedon Oct 08 '20 at 16:47
  • Adding split did work, I just need to add a new line in-between the results of the first for loop and the second. – Nicholas Fedon Oct 08 '20 at 16:51

1 Answers1

0

To slice of the first 3 numbers, just do print(numbers[3:]). There is no need for a loop unless you want to print them in a different format. But then you should do a loop like

for a in numbers[3:]:
  print(a)

Note that you do print(a) to print the element, not print(numbers) which prints the entire list.

Code-Apprentice
  • 76,639
  • 19
  • 130
  • 241