Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing only 1's and 0's

Link to the flowchart i made

I want to make a program that accepts a string of 1’s and 0’s. It should output ‘ is valid’ if and only if the string starts with 1, the second with 0, and the last be 1. The string can be of any length. If the string does not follow the conditions and is composed of letters or special characters, the program should state that is invalid. This is my coded version of this and it prints invalid when i type 101 and when i typed 10 it is valid.

x = str(input('Enter numbers: '))

if x == '10':
   print('string is valid')

else:
   print('Invalid Input')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is checking that first characters are 1 and 0, checking last one is 1 and the string does contain only 0 or 1 characters.

x = str(input('Enter numbers: '))

if x.startswith('10') and x.endswith('1') and all(letter in '01' for letter in x):
   print('string is valid')

else:
   print('Invalid Input')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading