-2

I'm trying to write a program that prints a hidden message the user has input. The message should be discovered by reading every 3rd character in the input. For example if I input: "pbaynatnahproarnsm" it should print out the bolded letters, i.e. "python". But it comes up with this:

Message? pbaynatnahproarnsm

p y t h o n Traceback (most recent call last):

  File "program.py", line 4, in <module>
    print(msg[x], end= " ")
IndexError: string index out of range

Here is the code

x = 0
msg = input("Message? ") 
for i in msg:
  print(msg[x], end= " ")
  x = x + 3
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • 3
    If you iterate over the whole string, which has length n, you'll end up accessing indices as far as 3*n. – Reti43 Jan 25 '21 at 01:23
  • 1
    This code prints the first letter, then the fourth, then the seventh, etc. However the loop iterates once for _each_ letter in the string, so `[x]` quickly advances past the end of the string. – John Gordon Jan 25 '21 at 01:33
  • Please don't be discouraged by people downvoting you, you asked your question really well with the full output and error message and a clear paragraph about what you're trying to do and what you've tried. – Boris Verkhovskiy Jan 27 '21 at 15:24

3 Answers3

3

You can use Python's slice notation to get every third letter in a string:

msg = "pbaynatnahproarnsm"
print(msg[::3])  # "python"
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • This is the ideal way to do it . But I suspect this is was a learning exercise and slices may not have been covered yet. – aneroid Jan 25 '21 at 01:56
0

seems like you've misunderstood how for loop works, the equlavent code with while loop:

x = 0
msg = input("Message? ") 
while x < len(msg):
  print(msg[x], end= " ")
  x = x + 3

here is the equlavent for loop:

msg = input("Message? ") 
for i in range(0, len(msg), 3):
  ## i will be 0, 3, 6, 9, 12, ... at each iteration
  c = msg[i]
  ## c will be the 0th, 3rd, 6th, ... character at each iteration
  print(c, end= " ")

even simpler

msg = input("Message? ") 
print(' '.join([msg[i] for i in range(0, len(msg), 3)]))

the output is : p y t h o n

thakee nathees
  • 877
  • 1
  • 9
  • 16
0

for i in msg: iterates over each character in msg. You're using x starting at 0 and incrementing by 3 in every loop - so when you do msg[x], the index x is 3 times the value of the index of the actual current character. Check by just printing x in your loop. When you're on the 8th char, with index 7, x is 21 which is longer than the length of msg (18).

To get the right output, the quickest/easiest way is using slice notation as per Boris' answer: msg[::3]. To get spaces, use str.join():

print(' '.join(msg[::3]))

If you want to use your method as a base, then increment x only by 1 (not 3) and print it only if its index is divisible by 3 (by checking that the remainder when divided by 3 is 0):

x = 0
for i in msg:
    if x % 3 == 0:  # `%` gives the remainder
        print(i, end=' ')
    x += 1  # increment by 1, not 3

# p y t h o n

But that's a complicated way to go about it. (Using enumerate() in the loop would be better; or just the slice notation, as mentioned.)

aneroid
  • 12,983
  • 3
  • 36
  • 66