I enter into the console a string of numbers. How to set the sign of the end of the input? For example, read until the character '0', etc.
2 answers
In [2]: for i in iter(input, 'stop'): ...: print('Вы ввели: ' + i) ...: hello Вы ввели: hello world Вы ввели: world stop From the documentation :
Return an iterator object. The first argument is interpreted very differently. It is a collection of objects that supports the iteration protocol (the
__iter__()method). If it doesn’t support either of those protocols,TypeErroris raised. The object is a callable object. The iterator is set to__next__()method; If the value is returned, it is aStopIterationwill be raised.
|
So, for example:
lines = [] while True: line = input('Введите строку: ') if line == '0': break else: lines.append(line) print(lines) |
'0'is encountered - jfs