M
M
michadimin2021-10-07 20:07:40
Python
michadimin, 2021-10-07 20:07:40

The output of all entered numbers, except 0, but the input can be continued after 0. I don’t understand: how to solve?

I beg you! Help solve this shit! I tried 3 different solutions, but none of them met ALL the requirements of the problem.

The user enters numbers one by one until they enter zero. The program should display the entered numbers until 0 is entered. No operations with numbers are performed.

Input format:
Integers are entered one after the other, on separate lines.

Output Format:
Prints entered numbers on separate lines until zero is entered

Example 1:
Input:
1
2
3
4
5
6
7
8
9
0
1
Output:
1
2
3
4
5
6
7
8
9

Example 2:
Input:
5
4
3
2
1
0
1
2
3
4
5
Output:
5
4
3
2
1

Please note! User input DOES NOT end with 0, but has the ability to continue! A task about the while loop.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-08
@Vindicar

Instead of input() read from sys.stdin as from a text file?

import sys
for line in sys.stdin:
  ... #обрабатываешь введённую строку

In Windows, you can press ctrl-z, Enter in the console, this will be the signal "end of standard input". It will be like the end of the file, and the for loop will break.
Accordingly, you simply make a logical flag "0 was entered" (it is reset by default), and while it is reset, you repeat the entered numbers. When it is exposed - do not repeat. If the user entered 0, set this flag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question