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.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Matt
  • 51
  • 1
  • 1
  • 2
  • ... And what if the user inputs something other than "True" or "false"? – Kevin Sep 16 '15 at 18:56
  • and why is one capitalized and one not? – Joran Beasley Sep 16 '15 at 18:59
  • this isn't totally comprehensive, but there is a handy lib for reading true false strings to booleans: https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python -- a yaml parser can also be used, Not sure I'd recommend that myself. – ThorSummoner Aug 09 '17 at 19:06

7 Answers7

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!")
jtb
  • 461
  • 1
  • 8
  • 22
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    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) – Joran Beasley Sep 16 '15 at 19:09
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")
erewok
  • 7,555
  • 3
  • 33
  • 45
2
johnnyHungry = input("Is johnny hungry ")
if johnnyHungry == "True":
...

I expect that you can take it from there?

Prune
  • 76,765
  • 14
  • 60
  • 81
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
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.

MS_092420
  • 5
  • 1
  • 1
  • 4
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

-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.

newDelete
  • 385
  • 2
  • 10