K
K
Kirill2021-11-21 23:09:53
Python
Kirill, 2021-11-21 23:09:53

How to check each digit in a number for compliance, and if the condition is true, then a certain sentence is displayed?

getNumber = input('Введите число: ')  # Ввод числа
x = 1
y = 0
var2 = getNumber[:1]
v = len(getNumber)
for i in range(v+1):
    if var2 == '1' or var2 == '0':
        var2 = getNumber[y:x]
        x += 1
        y += 1
        print("верно")
    else:
        print('Двоичное число может содержать только цифры 0 и 1')
        break

How to make it so that if the loop is executed correctly, then a certain text is displayed, and if not, then a different phrase is displayed.

Ps I apologize in advance for the wording of the question. I hope you can understand from the code what I want to do. :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kisaa, 2021-11-22
@kefirchik_tat

allowed = set('01')

def check(test_str):
    return set(test_str) <= allowed

print(check('010111010110'))
print(check('1264893215'))
print(check('ZXvn,mdg^&%32012mjcv'))

https://stackoverflow.com/questions/1323364/in-pyt...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question