Python 3 input Enter multiple lines

Asked 1 years ago, Updated 1 years ago, 124 views

print ("Enter bank transaction history (ex, D 200 or W 100) Ctrl-D to save it")
contents = []
while True:
    try:
        line = input()
    except EOFError:
        break
    contents.append(line)

If you press Ctrl-D after D 200 Enter and W 100 Enter in the same code as above, it will be appended to the list normally If you do Ctrl+D without entering W100, the list will not be appended normally.
Is there a way?

python input

2022-09-22 18:28

1 Answers

If you press Ctrl+D(EOF) while typing, the append(line) sentence does not execute because the exception leaves the while statement as break.
I think I need to put the append in finally: or something.

print ("Enter bank transaction history (ex, D 200 or W 100) Ctrl-D to save it")
contents = []
while True:
    try:
        If you type line = input() #W100 and press Ctrl+D without enter,
    EXCEPT EOFError: # Caught it here
        The sentence below cannot be executed because it leaves the break # while statement.
    contents.append(line)


2022-09-22 18:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.