Python Forum

Full Version: Using string input for boolean
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I've in the process of learning logical operators. I have the following code which works.

# high_income = input("Do you have a high income (yes/no)?: ")
# good_credit = input("Do you have good credit (yes/no)?: ")
# student = input("Are you a student (yes/no)?: ")
# if (high_income= or good_credit) and not student:
#     print ("Eligable")
# else:
#     print ("Not Eligable")
I'm working to modify the code to accept user input for the same outcome but this doesn't work.

high_income = input("Do you have a high income (yes/no)?: ")
good_credit = input("Do you have good credit (yes/no)?: ")
student = input("Are you a student (yes/no)?: ")

if high_income in (["yes"] or good_credit in ["yes"]): and not student in ["yes"]:
    print ("Eligable")
else:
    print ("Not Eligable")
However when I strip it down using a single variable input as a below it DOES work.

if high_income in (["yes"] or good_credit in ["yes"]): and not student in ["no"]:
    print ("Eligable")
else:
    print ("Not Eligable")
The outcome should be (Eligible) if high_income OR good_credit are True(yes) and NOT student. However the outcomes aren't what I would expect. I'm sure I've messed up somewhere but can't see why this won't work.

Thanks in advance.
Your punctuation is incorrect. Use parentheses to make the expression clearer
if (high_income in ["yes"]) or (good_credit in ["yes"]) and (student not in ["yes"]):
    print ("Eligable")
else:
    print ("Not Eligable")
(Oct-31-2023, 10:02 PM)Gribouillis Wrote: [ -> ]Your punctuation is incorrect. Use parentheses to make the expression clearer
if (high_income in ["yes"]) or (good_credit in ["yes"]) and (student not in ["yes"]):
    print ("Eligable")
else:
    print ("Not Eligable")

Ah awesome! I knew it was something like that. I found a solution that worked using == operators as below.

high_income = input("Do you have a high income (yes/no)?: ")
good_credit = input("Do you have good credit (yes/no)?: ")
student = input("Are you a student (yes/no)?: ")

if high_income == ("yes") or good_credit == ("yes") and not student in ["yes"]:
    print ("Eligable")
else:
    print ("Not Eligable")
Is one either one of these solutions "better" than the other?

Thanks for your help!
(Oct-31-2023, 10:51 PM)tronic72 Wrote: [ -> ]Is one either one of these solutions "better" than the other?
No, they are equally good. You could improve the user interaction by allowing answers like yes YES Yes y Y etc. Also you could provide a default, allowing the user to make a choice by just hitting the Return key.
def yesno_input(msg, default=True):
    end = "YES/no" if default else "yes/NO"
    x = input(f"{msg} ({end}) ").strip().lower()
    if not x:
        return default
    elif x in ("yes", "y"):
        return True
    elif x in ("no", "n"):
        return False
    else:
        raise ValueError(x)


has_high_income = yesno_input("Do you have a high income?")
has_good_credit = yesno_input("Do you have good credit?")
is_student = yesno_input("Are you a student?", default=False)

if has_high_income and has_good_credit and not is_student:
    print("Eligible")
else:
    print("Not Eligible")
Output:
Do you have a high income? (YES/no) y Do you have good credit? (YES/no) Are you a student? (yes/NO) Eligible