0

I have a list of letters. As of right now, I am able to allocate my starting point based on the index point (IE: 0 = 'A', 1 = 'B', 2 = 'C', etc).

def moving_letters():
    letters = ['A','B','C','D','E','F','G']
    *user = int(input('Enter starting index #: '))* 
    print(letters[user], letters[(user + 5) % len(letters)], letters[(user + 7) % len(letters)])


print(moving_letters())  

How would I be able to select a specific letter in my list as a starting point?

Also, how would I be able to move my starting point as showed in?:

print(letters[user], letters[(user + 5) % len(letters)], 
    letters[(user + 7) % len(letters)])
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
poptart20
  • 63
  • 6
  • 2
    It is not clear what you are trying to do, but your function does not return anything. – DYZ Jan 12 '22 at 03:31
  • what do you mean? I bolded my questions. – poptart20 Jan 12 '22 at 03:35
  • and what exactly is the problem? also note that `(x+n)%n` is the same as `x%n` that in your particular example that is n=7 – Copperfield Jan 12 '22 at 03:51
  • perhaps the issue is that you don't show the user what are the options? if so, well just show the user what are the options, beside that I don't see anything particularly wrong with that sample code (ignoring the `*` of course) tho is unclear what the goal of the function is – Copperfield Jan 12 '22 at 04:01
  • If the function does its own printing, you shouldn't call `print()` again when calling it. You're just going to print an extra `None`. – Barmar Jan 12 '22 at 04:17
  • If you want to let the user select the letter instead of index, you can use `letters.index(user)` to get its index. – Barmar Jan 12 '22 at 04:18
  • @Barmar, yes I want the user to select the letter and then move down the list based on the selected letter. – poptart20 Jan 12 '22 at 04:55
  • So what problem are you having? Doesn't the `index()` method work for that? – Barmar Jan 12 '22 at 16:50

1 Answers1

1

You can select a slice of a list with : in the index, where you can specify start and end, if either is left empty you select everything. After you have a slice of a list selected, you can use join to make a line to print. Keep in mind the items you are joining have to be strings.

def moving_letters():
    letters = ['A','B','C','D','E','F','G']
    user = int(input('Enter starting index #: '))
    s = " ".join(letters[user:])
    print(s)

moving_letters()

Slice notation: Understanding slice notation

String join: What exactly does the .join() method do?

Tzane
  • 2,752
  • 1
  • 10
  • 21