0

I am building a simple program to pre-screen tenants for my rental properties. The program asks them a series of questions - some of which require a "yes" or "no" answer which would be a boolean (true/false).

The problem is, no matter what you answer for the boolean inputs it records as "1".

I'm using sqlite3 locally to store the data, here's the code:

def enter_dynamic_data():
     fname = input("First Name? ")
     lname = input("Last Name? ")
     email = input("Email? ")
     phone = input("Phone? ")
     criminal = bool(input("Have you ever been convicted of a crime? "))
     evicted = bool(input("Have you ever been evicted? "))
     income = bool(input("Do you have verifiable income of at least 3x the rent amount? "))
     ref = bool(input("Do you have good rental references? "))

     c.execute("INSERT INTO tenant_screening (firstname, lastname, email, phone, criminal, evicted, income, ref) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (fname, lname, email, phone, criminal, evicted, income, ref))
freefly0313
  • 105
  • 4
  • 14
  • It's because bool() function evaluates everything to True (1) except things like empty lists/tuples, None, False, or empty strings. Saying bool("anything") is always evaluated to True, because there is something. – Nf4r Oct 02 '16 at 23:56
  • Pretty much what the duplicate is stating is that sqllite3 bools are stored as 0, 1. *SQLite does not have a separate Boolean storage class*. [doc](http://www.sqlite.org/datatype3.html) for reference – idjaw Oct 02 '16 at 23:58
  • As easy as: answer = input().lower() == "yes";. You can also do other checking as: answer = int(input()) > 100; etc.. – dinomoth Jul 05 '17 at 22:06

2 Answers2

1

In Python any non-empty string is considered True so regardless of the user's input all non-empty strings entered by the user will be True when converted to boolean.

>>> bool('true')
True
>>> bool('false')
True
>>> bool('')
False

So you need to compare the value entered by the user against a set of values that represent True, e.g.

YES_VALUES = {'y', 'yes', 'ok'}

criminal = input("Have you ever been convicted of a crime? ").lower() in YES_VALUES

This converts the user's input to lower case and then checks whether that value is in the set YES_VALUES. The result of x in y is already a boolean, so explicit conversion via bool is not required.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

Wrapping the input in Boolean will always return True if the input is not None. Instead you could do something like:

response = False if raw_input("The yesno question? ").lower() == 'no' else True
Matz
  • 371
  • 1
  • 2
  • 5