I am trying to solve a python programming challenge that requires a program for checking if a string is binary. If the string is binary, it should return "true". Otherwise, it should return "false".
When I ran the code, it iterates through the string and prints either "true" or "false" depending on whether the value in the string is "0" and "1" or not. Even though I have tried a couple of methods I keep ending up with a vertical display of the boolean values.
binary = {'0', '1'}
def is_binary(string):
for i in str(string):
if i in binary:
print('true')
else:
print('false')
break
is_binary('101010')
is_binary('101210')
How can I modify the code to be able to print a single "true" statement when the string is binary and a single "false" statement when the string is not binary regardless of the length of the string?
return False. If you reached the end of the string, it means that all digits are binary, hencereturn True