0

Ввожу в консоль строку из чисел. Как задать признак конца ввода? Например, считывать до тех пор, пока не будет символ '0' и т.д

1

2 ответа 2

5
In [2]: for i in iter(input, 'stop'):
   ...:     print('Вы ввели: ' + i)
   ...:
hello
Вы ввели: hello
world
Вы ввели: world
stop

Из документации:

Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its __next__() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

0

Вот так, например:

lines = []

while True:
    line = input('Введите строку: ')

    if line == '0':
        break
    else:
        lines.append(line)

print(lines)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.