0

I have to do this for school, but I can't work it out. I have to get an input and print ONLY the odd characters. So far I've put the input in a list and I have a while loop (which was a clue on the task sheet), but I can't work it out. Please help:

inp = input('What is your name? ')
name = []
name.append(inp)
n=1

while n<len(name):
George Baker
  • 91
  • 1
  • 3
  • 13
  • I know it may not seem like the same thing but take a look at that question. It's useful because the OP does the "modulo trick" (`%2`), which is one common way to do this, and the accepted answer uses slicing, which can be used on strings as well as lists. – Two-Bit Alchemist Nov 20 '15 at 18:39

2 Answers2

5
print inp[1::2]

I guess thats all you need

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
3

You don't need to put the string in a list, a string is already essentially a list of characters (more formally, it is a "sequence").

You can use indexing and the modulus operator (%) for this

inp = input('What is your name? ')
n = 0   # index variable

while n < len(inp):
    if n % 2 == 1:      # check if it is an odd letter
        print(inp[n])   # print out that letter
    n += 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218