5
votes

I am trying to have a user input whether or not they like spicy food and the output is supposed to be a boolean but I don't seem to be getting an output with my code below:

def likes_spicyfood():
    spicyfood = bool(input("Do you like spicy food? True or False?"))
    if spicyfood == ("True"):
        print("True")
    if spicyfood == ("False"):
        print("False")
        return(likes_spicyfood)
10
Hint: Test with an empty input. - heemayl
Well: 1. any non-empty string input will evaluate truth-y; and 2. neither Boolean value is equal to either of the strings "True" or "False". Also the parentheses in that equality evaluation are redundant. You could just skip the bool; "True" == "True". - jonrsharpe
Why are you converting the user input to boolean, then comparing it to strings? Just don't convert it. - Christian Dean
Remove the bool statement before input should work fine - mgracer
Also if you want to return Boolean values why are you printing strings ('True'). Instead use Return True - Moller Rodrigues

10 Answers

10
votes

Trying to convert your input to bool won't work like that. Python considers any non-empty string True. So doing bool(input()) is basically the same as doing input() != ''. Both return true even if the input wasn't "True". Just compare the input given directly to the strings "True and "False":

def likes_spicyfood():
    spicyfood = input("Do you like spicy food? True or False?")
    if spicyfood == "True":
        return True
    if spicyfood == "False":
        return False

Note that the above code will fail (by returning None instead of a boolean value) if the input is anything but "True or "False". Consider returning a default value or re-asking the user for input if the original input is invalid (i.e not "True or "False").

3
votes

In your usage, converting a string to a bool will not be a solution that will work. In Python, if you convert a string to a bool, for example: bool("False") the boolean value will be True, this is because if you convert a non-empty string to a bool it will always convert to True, but if you try to convert an empty string to a bool you'll get False.

To solve your issue several changes have to be made. First off your code sample does not even call the function where you ask the user whether they like spicy food or not, so call it on the very bottom of the code. likes_spicyfood()

Second thing you'll have to change is that you'll have to simply have the user type True or False like you have in your code, but instead of converting the value from string to bool, simply take the string and compare it to either 'True' or 'False', here is the full code:

def likes_spicyfood():
    spicyfood = input("Do you like spicy food? True or False?")
    if spicyfood == "True":
        print("The user likes spicy food!")
    if spicyfood == "False":
        print("The user hates spicy food!")
    return likes_spicyfood

likes_spicyfood()

You'll also see that I've returned some redundant parenthesis: when comparing the input value to 'True' or 'False' and when returing likes_spicyfood. Here's more on converting a string to a bool

3
votes

If you are certain input is correct you can do:

def likes_spicyfood():
    spicyfood = input("Do you like spicy food? True or False?")
    return spicyfood.title() == "True"
1
votes

Don't cast the input as a bool. Later make a condition that checks whether it is True or False

Something like this :)

def likes_spicyfood():
spicyfood = input("Do you like spicy food? True or False?")
while spicyfood!= "True" or "False":
    spicyfood=input("Do you like spicy food? True or False?")
if spicyfood == ("True"):
    print("True")
if spicyfood == ("False"):
    print("False")
    return(likes_spicyfood)
1
votes

Try this. Ask for answer in 0 and 1 and then convert that to boolean value. Like I did in the example below

isTrue = True
while isTrue:
  isTrue = bool(int(input("Continue? 1 for yes, 0 for no: ")))
0
votes

As Christian Dean pointed, bool(input()) will return True for any not-null input. If you really need to use a bool, you could force it this way:

def likes_spicyfood():
    spicyfood = input("Do you like spicy food? True or False?")
    if(spicyfood=="True"):
        spicyfood_Bool = True
    elif(spicyfood=="False"):
        spicyfood_Bool = False
    if(spicyfood_Bool):
        print("True")
    elif(!spicyfood_Bool):
        print("False")

    return(likes_spicyfood)

This is a workaround, "translating" the input string to a bool (spicyfood_Bool). No need to say that this solution is overcomplicated, but it will do de job if you really need a bool.

0
votes

you can simply use this

inp=input('Do you like spicy food? True or False?')
spicyfood = True if inp== 'True' else  False if inp=='False'
0
votes
spicyfood = bool(input("Do you like spicy food? t/f?")=='t')
0
votes

First we'll need to read the user's input after they read the question:

hungry = input("Are you hungry? True or False \n")

Now that we have their response in a variable, hungry, we're able to use it in some statements:

if hungry.lower() == "true":

Notice the use of .lower(), which will take their string, regardless of capitalisation of letters, and convert it all to lowercase. This means that whether they enter "True", "tRue" or "TRUE", they will all evaluate to being True if compared to the lowercase counterpart "true".

What's important when doing this, is to always have your comparison string be lowercase as well. because if you tried evaluating "true" against "True", it would return False.

Using this knowledge, we're able to put it all together and print out what we want:

hungry = input("Are you hungry? True or false \n")

if hungry.lower() == "true":
    print("Feed me!")
else:
    print("I'm not hungry.")
0
votes

You could also use module "ast" with "literal_eval"-Method. Like this:

import ast

bool = ast.literal_eval(input("True or False?"))