darkprinx/break-the-ice-with-python

Correct solution to Question 9

Euan-J-Austin opened this issue · 0 comments

First, of all, I would like to thank you for creating this valuable resource and others for contributing.

Concerning Question 9, neither of the provided solutions produces the output specified in the question:

Solution 1 does not work because the program is exited before the input is appended to the list.

lst = []

while input():
    x = input()
    if len(x) == 0:
        break
    lst.append(x.upper())

for line in lst:
    print(line)

Solution 2 works in part. The user can enter a single line and a capitalized line is returned. This is not the output specified in the solution. Moreover, there's no way to break from the code without initiating an EOFError using ctrl+d/z.


def user_input():
    while True:
        s = input()
        if not s:
            return
        yield s


for line in map(str.upper, user_input()):
    print(line)

I believe my solution gives the output specified in Question 9, with the empty line separating the two.

lines = []
print("Enter your text, when finished type 'end_and_print':\n")
while True:
    line = input()
    if line != 'end_and_print':
        lines.append(line)
    else:
        break
print('\n'.join(lines).upper())