0
x = bool(input ( "Write True or False\n"))

if  x is True :
  print("its true")
elif x is False :
    print ("its false")

when I write True or False I always get "its true". i want the program to say "its false" when i type False.

4
  • 3
    I cannot think of any type that will fall into your else case. Commented Dec 3, 2014 at 16:47
  • Depending how specific you want to be keeping it a string and checking against that string may be the way to go. Commented Dec 3, 2014 at 16:48
  • Is there a reason you don't want to say if x == "True"? In python, an empty String will be False, but any String will text in it will evaluate to True. Commented Dec 3, 2014 at 16:48
  • "poolean"? Reminds me of "Prian". Commented Dec 3, 2014 at 16:54

2 Answers 2

1

You'll have a problem with converting to bool because technically any non-empty string will evaluate to True

>>> bool('True')
True
>>> bool('False')
True
>>> bool('')
False

You can use ast.literal_eval though

>>> from ast import literal_eval
>>> literal_eval('True')
True
>>> literal_eval('False')
False

Otherwise you'll have to compare the actual strings themselves

x = input("Write True or False\n")

if x in ('True', 'true'):
  print("its true")
elif x in ('False', 'false'):
    print ("its false")
else:
    print("don't know what that is")  
Sign up to request clarification or add additional context in comments.

Comments

0

I'd just compare the strings:

x = input("Write True or False\n")

if x == "True":
  print("its true")
elif x == "False":
    print ("its false")
else:
    print("don't know what that is") 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.