0

I want to allow only 1 and 0 inputs for user. How do I do that?

num2=int(input("Is Big Nose true or false? (1/0): "))
num3=int(input("Is Bandana true or false? (1/0): "))
num1=int(input("Is Spots true or false? (1/0)"))

if (num1 and num2 and num3 and num4):
    print("Becky")
elif (num1 or num2 and num3 and num4):
    print("Tara")
elif (num3 and num4):
    print("Keri")
else:
    print("Laura")
NameError
  • 206
  • 1
  • 3
  • 19
  • 4
    Please post your actual code in the question, not just an image. This question also not related to PHP whatsoever. – pigeonburger Aug 18 '21 at 09:57
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – pigeonburger Aug 18 '21 at 09:57
  • • Welcome to StackOverflow! Please avoid uploading code as a picture as an image. https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – nima Aug 18 '21 at 10:00
  • please take a moment and read this article https://stackoverflow.com/help/how-to-ask about how to asking questions also read this article https://stackoverflow.com/help/minimal-reproducible-example about how to ask a good question with minimum requirement. – nima Aug 18 '21 at 10:01
  • I guess the answer is in [here](https://stackoverflow.com/questions/8761778/limiting-python-input-strings-to-certain-characters-and-lengths). I hope this helps. – NameError Aug 18 '21 at 11:37

4 Answers4

1

You can try using loop for getting the desired value. If the user enters the desired one, the loop is broken else it will continue to ask until right input is provided. Refer this code for more info:

while True:
try:
    num1 = int(input("Enter your choice "))
    if num1 == 0 or num1 == 1:
        print("Entered choice ",num1)
        break
    else:
        print("Please enter between 0 or 1 only")
        continue
except ValueError as e:
    print("Please enter in correct format")
    continue

You can try the same for the other inputs as well.

Dharman
  • 30,962
  • 25
  • 85
  • 135
YamanSinghal
  • 132
  • 1
  • 4
1
while True:
print('1 or 0')
A=int(input())
if A!=int(1) and A!=int(0) :
    continue
else:
    break

print ('okay') as you see i use a while loop so that if he didnt put 1 or 0 the loop will restart and it print 1 or 0 till he put 1 or 0 and it will put okay

0

You can try this if you want to display the user an error message on the console:

num1 = int(input("Is Blue Hair (1/0): "))
if num1 == 0:
    print("Number 0")
elif num1 == 1:
    print("Number 1")
else:
   print("Enter Valid No:")
  • If this answer helpful then upvote this answer so next time if anyone face same problem can check the verified answer. – Pulkit Arora Aug 18 '21 at 13:31
0

you can also consider this one. if you want to initialize something in list with 'x' number of length.

list1=[]
def cal(i):
    for j in range(0,i):
        k=int(input('enter the value'))
        list1.append(k)
    for l in list1:
        if l==0 or l==1:
            print('ok thats fine ')
        else:
            pass

i=int(input('enter the numbers'))
cal(i)
print(list1)
RITIK KUMAR
  • 137
  • 1
  • 7