Python Forum
Using string input for boolean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using string input for boolean
#1
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.
Reply
#2
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")
Reply
#3
(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!
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,153 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Big Grin General programming question (input string)[ jamie_01 2 1,609 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Creating new column with a input string drunkenneo 2 2,267 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,840 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  user input producing incorrect boolean al_Czervik 4 3,089 Mar-05-2020, 09:50 PM
Last Post: al_Czervik
  input string split Eric7Giants 3 3,036 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 2,602 Oct-12-2019, 09:25 AM
Last Post: konsular
  simple string & input problem kungshamji 5 3,679 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  How to use a string method on user input Exsul 2 2,649 Mar-17-2019, 08:12 PM
Last Post: Exsul
  Mixed string,Integer input variable issue maderdash 2 2,765 Nov-06-2018, 09:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020