4

I'm new to python.

I want the program to ask

"is Johnny hungry? True or false?"

user inputs True then print is "Johnny needs to eat."

user inputs false then print "Johnny is full."

I know to add an int I type in

johnnyHungry = int(input("Is johnny hungry ")) 

but I want them to enter True/false, not an int.

3
  • ... And what if the user inputs something other than "True" or "false"? Commented Sep 16, 2015 at 18:56
  • and why is one capitalized and one not? Commented Sep 16, 2015 at 18:59
  • this isn't totally comprehensive, but there is a handy lib for reading true false strings to booleans: stackoverflow.com/questions/715417/… -- a yaml parser can also be used, Not sure I'd recommend that myself. Commented Aug 9, 2017 at 19:06

7 Answers 7

13

you can use a simple helper that will force whatever input you want

def get_bool(prompt):
    while True:
        try:
           return {"true":True,"false":False}[input(prompt).lower()]
        except KeyError:
           print("Invalid input please enter True or False!")

print get_bool("Is Jonny Hungry?")

you can apply this to anything

def get_int(prompt):
    while True:
        try:
           return int(input(prompt))
        except ValueError:
           print("Thats not an integer silly!")
Sign up to request clarification or add additional context in comments.

1 Comment

but then it can return something other than T/F ... I think OP wants the return to be forced (but I thought about something like that too :P good suggestion all the same)
11

You can turn something into True or False using bool:

>>> bool(0)
False
>>> bool("True")
True
>>> bool("")
False

But there is a problem with a user entering "True" or "False" and assuming we can turn that string into a bool:

>>> bool("False")
True

This is because a string is considered truthy if it's not empty.

Typically, what we do instead is allow a user to enter a certain subset of possible inputs and then we tell the user that only these values are allowed if the user enters something else:

user_answer = input("Is johnny hungry ").lower().strip()
if user_answer == "true":
    # do something
elif user_answer == "false":
    # do something else
else:
    print("Error: Answer must be True or False")

2 Comments

Thank you. I thought yours was good as well but it could be a bit tough for a brand new Pythoner.
although it would probably be better to if answer == "true":...elif answer == "false":... else:print "ERROR"
2
johnnyHungry = input("Is johnny hungry ")
if johnnyHungry == "True":
...

I expect that you can take it from there?

1 Comment

good answer for a python beginner even if it is not super robust :P +1
0
def get_bool(prompt):
while True:
    try:
        return {"si": True, "no": False}[input(prompt).lower()]
    except KeyError:
        print ("Invalido ingresa por favor si o no!")

    respuesta = get_bool("Quieres correr este programa: si o no")

if (respuesta == True):
    x = 0
else:
    x = 2

Comments

0

I hope this code works for you. Because I already tried this code and it worked fine.

johnnyHungry = str(input("Is johnny hungry ")) 

def checking(x):
    return 'yes' == True
    return 'no' == False

if checking(johnnyHungry) == True:
    print("Johnny needs to eat.")
elif checking(johnnyHungry) == False:
    print("Johnny is full.")

I'm sorry if it's long, I'm new to programming either.

1 Comment

Actually newDelete's answer is not wrong and it's almost the same as mine.
0

Code:

question = str(input("Is Johnny hungry?  Response: "))

if question == "yes":

    print("Johnny needs to eat food!")

if question == "no":

    print("Johnny is full!")

Run:

  1. Is Johnny hungry? Response: yes

    Johnny needs to eat food!

  2. Is Johnny hungry? Response: no

    Johnny is full!

  3. This is if you type something other than yes and no

    Is Johnny hungry? Response: lkjdahkjehlhd

Nothing pops up/ there is no response

Comments

-3

You could just try bool(input("...")) but you would get into trouble if the user doesn't input a bool. You could just wrap it in a try statement.

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.