Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct solution to Question 9 #131

Open
Euan-J-Austin opened this issue Feb 28, 2023 · 0 comments
Open

Correct solution to Question 9 #131

Euan-J-Austin opened this issue Feb 28, 2023 · 0 comments

Comments

@Euan-J-Austin
Copy link

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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant