0

I am trying to make a simple little program using Python 3.7.4. I was wondering if it is possible (If so, how?) to change the Boolean value of a variable from a user input.

As you can see in the attached code, I have tried putting the append in quotations. I also tried no quotations.

    is_male = []
    is_female = []
    is_tall = []
    is_short = []

    ans_male = bool(input("Are you a male? (True or False): "))
    if ans_male == "True":
        is_male.append("True")
    if ans_male == "False":
        is_male.append("False")
    ans_female = bool(input("Are you a female? (True or False): "))
    if ans_female == "True":
        is_female.append("True")
    if ans_female == "False":
        is_female.append("False")
    ans_tall = bool(input("Are you tall? (True or False): "))
    if ans_tall == "True":
        is_tall.append("True")
    if ans_tall == "False":
        is_tall.append("False")
    ans_short = bool(input("Are you short? (True or False): "))
    if ans_short == "True":
        is_short.append("True")
    if ans_short == "False":
        is_short.append("False")

I expect the is_male, is_tall, etc. variable's value to change to True or False according to the input.

1
  • do you really want to append the list with "True"/"False" (type --> str) instead of True/False (Type --> bool) Commented Aug 17, 2019 at 19:16

7 Answers 7

1

You are getting a few things wrong.

1) When transforming a string into a boolean, python always returns True unless the string is empty (""). So it doesn't matter if the string is "True" or "False", it will always become True when you do bool(str).

2) In your if statements, you're comparing a boolean (the ans_male variable) with a string ("True"). If you want to work with booleans, you should write only True or False, without the quotation marks.

In your case, as you're reading a value from the input(), it is much easier to just leave it as a string and compare it with "True" and "False" instead. So you could do as follow:

ans_male = input("Are you a male? (True or False): ")
if ans_male == "True": # Check if string is equal to "True"
    is_male.append(True) # Appends the boolean value True to the list.
Sign up to request clarification or add additional context in comments.

Comments

0

Here ans_male will have true value even if your input is false. You can't convert string to bool value like this. Better create a function to convert string into bool values as following.

def stringToBool(ans_male):
    if ans_male == 'True':
       return True
    else if ans_male == 'False'
       return False
    else:
       raise ValueError

Comments

0

Booleans don't have quotation marks, those are only for strings. The following is 1/4 of your code with this change applied:

    is_male = []

    ans_male = bool(input("Are you a male? (True or False): "))
    if ans_male == "True":
        is_male.append(True)
    if ans_male == "False":
        is_male.append(False)

Note: ans_male == "True" has the boolean in string format because values assigned using input() are always strings.

Because of this, you may want to use the following instead (because follow):

is_male = []
    is_female = []
    is_tall = []
    is_short = []

    ans_male = bool(input("Are you a male? (Yes or No): "))
    if ans_male == "Yes":
        is_male.append(True)
    if ans_male == "No":
        is_male.append(False)
    ans_female = bool(input("Are you a male? (Yes or No): "))
    if ans_female == "Yes":
        is_female.append(True)
    if ans_female == "No":
        is_female.append(False)
    ans_tall = bool(input("Are you a male? (Yes or No): "))
    if ans_tall == "Yes":
        is_tall.append(True)
    if ans_tall == "No":
        is_tall.append(False)
    ans_short = bool(input("Are you a male? (Yes or No): "))
    if ans_short == "Yes":
        is_short.append(True)
    if ans_short == "No":
        is_short.append(False)

Comments

0

is_male.append(True) should work. I tested it and it works for me on v3.7.4.

Comments

0

Consider this code. You have a list of questions you want to ask, paired with the associated property (is_male, etc.). You have a dictionary that maps user input to a boolean. For each question, you print the current question and wait for the user to enter something. If they enter something invalid (something other than "True" or "False"), the while-loop will keep asking the same question until the user answers satisfactorily. When the user has answered all questions we print the results.

questions = [
    ("Are you male?", "is_male"),
    ("Are you tall?", "is_tall")
]

input_to_bool = {
    "True": True,
    "False": False
}

user = {}

for question, key in questions:
    while True:
        user_input = input(f"{question}: ")
        if user_input in input_to_bool:
            user.update({key: input_to_bool[user_input]})
            break

for key, value in user.items():
    print(f"{key}: {value}")

Output:

Are you male?: True
Are you tall?: False
is_male: True
is_tall: False

Comments

0

Your code transform the user input to bool.

ans_male = bool(input("Are you a male? (True or False): "))

So any input will be True and no input will be False.

How about doing something like:

is_male = []
ans_male = input('Are you a male? (Y or N): ')

if ans_male.upper() == 'Y':
    is_male.append(True)

elif ans_male.upper() == 'N':
    is_male.append(False)

else:
    print('Please choose Y or N')
    pass

Comments

0

Put your less affords and time with this code :

    is_male = []
    is_female = []
    is_tall = []
    is_short = []

    que = "Are you {} (Yes/No) : "

    ans_male = True if input(que.format("a male?")).lower()=='yes' else False
    is_male.append(str(ans_male))
    #if you want to append list with 'True'/'False' (type->str)

    ans_female = True if input(que.format("a female?")).lower()=='yes' else False
    is_female.append(ans_female)
    #if you want to append list with True/False (type -> bool)

    ans_tall = True if input(que.format("tall?")).lower()=='yes' else False
    is_tall.append(ans_tall)

    ans_short = True if input(que.format("short?")).lower()=='yes' else False
    is_short.append(ans_short)

It is the same code that you post in the question with efficient solution.

And yes an answer should be Yes/No because True/False sometime looks weird for this type of questions.

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.