【发布时间】:2016-10-02 23:47:06
【问题描述】:
我正在构建一个简单的程序来为我的出租物业预先筛选租户。该程序会问他们一系列问题——其中一些需要“是”或“否”的答案,这将是一个布尔值(真/假)。
问题是,无论您对记录为“1”的布尔输入做出什么回答。
我在本地使用 sqlite3 来存储数据,代码如下:
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))
【问题讨论】:
-
这是因为 bool() 函数将所有内容评估为 True (1),除了空列表/元组、None、False 或空字符串。说 bool("anything") 总是被评估为 True,因为有些东西。
-
重复的内容几乎是 sqllite3 布尔值存储为 0、1。SQLite 没有单独的布尔存储类。 doc供参考
-
就这么简单:answer = input().lower() == "yes";。您还可以进行其他检查: answer = int(input()) > 100;等等。