2015-06-20 138 views
1

想知道是否有方法將布爾值從true/false更改爲in/out?重命名true false布爾值

下面我的代碼是一個布爾開關,但我希望它或退出不爲0或1

while True: 
     Tag = raw_input("Please Scan your Tag: ") 
     q = "SELECT * FROM CARDS WHERE TAG=?" 
     up = "UPDATE CARDS SET FLAG = (CASE WHEN FLAG=0 THEN 1 ELSE 0 END) WHERE TAG=?" 
     cursor.execute(q, (Tag,)) 
     cursor.execute(up, (Tag,)) 
     conn.commit() 
     for row in cursor: 
     print row [1] + " has been checked " + row[3] 

回答

3

您可以使用'in' if condition else 'out'打印:

print row [1] + " has been checked " + ('in' if row[3] else 'out') 
+0

所以會看起來像。 'in'if flag = 1?你可以發佈示例 – shaggs

+0

@shaggs Python自動將零轉換爲「False」,非零轉換爲「True」。我已經更新了我的答案。 – StenSoft

+0

謝謝你的工作。仍在學習python和sqlite – shaggs

0

相反的:

print row [1] + " has been checked " + row[3] 

Try:

print row [1] + " has been checked " + ("in" if row[3] else "out") 
+0

謝謝你的回答,但是打到了拳頭 – shaggs

相關問題