Printing the letter in a line + 1 [closed]
Printing the letter in a line + 1 [closed]
Problem:
You and your friends have created a secret code!
The secret code is hidden in many lines. It's the first letter of the
first line, plus the second letter of the second line, and so on, like
this:
Basically it's a secret Won't they find it? Who would be looking? Nah!
It's fine. B + o + o + ! spells BOO!
Your program should work like this:
Line: Basically it's a secret
Line: Won't they find it?
Line: Who would be looking?
Line: Nah! It's fine.
Line:
BOO!
Your program should read multiple lines of input, get the correct
letter from each line. Then it should join each letter and print it in
capitals.
So far, I have this code which gets input, adds each line to a list then when the user enters a space it stops. But I'm a bit unsure on how to get it so the first letter from the first line is printed, the second letter from the second line is printed, the third letter from the third line is printed etc..
listo =
line = input("Line: ")
while line:
listo.append(line)
line = input("Line: ")
print(test)
for i in listo:
print(i[0+1])
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3 Answers
3
This might work:
lines = [ '123', 'abc', 'def' ]
print ''.join( b[a] for a,b in enumerate(lines) ).upper()
prints:
1BF
You can implement this by iterating through the index generated by enumerate
:
enumerate
from functools import partial
print(''.join(s[i] for i, s in enumerate(iter(partial(input, 'Line: '), ''))).upper())
To get the first letter from the first line and +1 for each other line, you will want to keep track of the current index as you are going through your loop. There is an edge case you will have to think about though where the line that was entered is not long enough to get the ith character from.
listo =
line = input("Line: ")
while line:
listo.append(line)
line = input("Line: ")
# Loop from index = 0 to len(listo)
for index in range(0, len(listo)):
cur_line = listo[index]
# If current index is less then length of the line
if index < len(cur_line):
print(cur_line[index]) # Print out the ith character from the line
You don't have to save everything the user inputs by the way.
– HSK
Aug 26 at 2:22