如何用一个输入创建不同的条件 (python)
how to make a different conditions with one input (python)
谁能帮我写代码?
我想将 RFID 卡数据插入 MySQL 数据库。
当我第一次点击 RFID Reader 上的标签时,程序会将数据插入 Masuk table 当我再次点击标签时,程序会将数据插入 Keluar table.
我用这段代码做对了吗?
import MFRC522
import signal
import time
import MySQLdb
import datetime
db = MySQLdb.connect(host='localhost',
user='root',
passwd='12345678',
db='pa')<br>
cursor = db.cursor()
continue_reading = True
MIFAREReader = MFRC522.MFRC522()
cardA = [131,89,173,1,118]
def read ():
read = 1
def end_read(signal, frame):
global continue_reading
continue_reading = False
print "Ctrl+C captured, ending read."
MIFAREReader.GPIO_CLEEN()
signal.signal(signal.SIGINT, end_read)
while continue_reading:
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
if status == MIFAREReader.MI_OK:
print "Card detected"
(status,backData) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
print "Card read UID: "+str(backData[0])+""+str(backData[1])+""+str(backData$
if backData == cardA:
print "Selamat Datang Dheny"
if (read == True):
sql = """ INSERT INTO Masuk(Nama, No_ID, datetime) VALUES ('Dheny', $
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
read = False
if (read == False):
sql = """ INSERT INTO Keluar(Nama, No_ID, datetime) VALUES ('Dhe$
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
read = True
你只需要像 this.create 这样的全局变量(计数)来实现 if and elif
条件下的行为,例如
if (count==True):
# insert into musak and update count to false
elif(count==False):
# insert into other table and update count to True
我不知道您是否尝试使用 read
变量来实现该行为,如果是,那么它应该是全局的并且使用 elif
而不是 if
来只执行一个情况下,您需要像下面这样更改 read
的值。
if (read == True):
sql = """ INSERT INTO Masuk(Nama, No_ID, datetime) VALUES ('Dheny', $
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
global read # Needed to modify global copy of read
globvar = False # next time read==False case will be executed
elif (read == False):
global read # Needed to modify global copy of read
read = True # next time read==True case will be executed
sql = """ INSERT INTO Keluar(Nama, No_ID, datetime) VALUES ('Dhe$
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
查看代码访问和修改全局变量
global count
def modi():
global count # required to modify the global copy
count=10
def printc():
if (count==10): # read it as a normal variable
print("yes")
modi()
printc()
谁能帮我写代码?
我想将 RFID 卡数据插入 MySQL 数据库。 当我第一次点击 RFID Reader 上的标签时,程序会将数据插入 Masuk table 当我再次点击标签时,程序会将数据插入 Keluar table.
我用这段代码做对了吗?
import MFRC522
import signal
import time
import MySQLdb
import datetime
db = MySQLdb.connect(host='localhost',
user='root',
passwd='12345678',
db='pa')<br>
cursor = db.cursor()
continue_reading = True
MIFAREReader = MFRC522.MFRC522()
cardA = [131,89,173,1,118]
def read ():
read = 1
def end_read(signal, frame):
global continue_reading
continue_reading = False
print "Ctrl+C captured, ending read."
MIFAREReader.GPIO_CLEEN()
signal.signal(signal.SIGINT, end_read)
while continue_reading:
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
if status == MIFAREReader.MI_OK:
print "Card detected"
(status,backData) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
print "Card read UID: "+str(backData[0])+""+str(backData[1])+""+str(backData$
if backData == cardA:
print "Selamat Datang Dheny"
if (read == True):
sql = """ INSERT INTO Masuk(Nama, No_ID, datetime) VALUES ('Dheny', $
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
read = False
if (read == False):
sql = """ INSERT INTO Keluar(Nama, No_ID, datetime) VALUES ('Dhe$
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
read = True
你只需要像 this.create 这样的全局变量(计数)来实现 if and elif
条件下的行为,例如
if (count==True):
# insert into musak and update count to false
elif(count==False):
# insert into other table and update count to True
我不知道您是否尝试使用 read
变量来实现该行为,如果是,那么它应该是全局的并且使用 elif
而不是 if
来只执行一个情况下,您需要像下面这样更改 read
的值。
if (read == True):
sql = """ INSERT INTO Masuk(Nama, No_ID, datetime) VALUES ('Dheny', $
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
global read # Needed to modify global copy of read
globvar = False # next time read==False case will be executed
elif (read == False):
global read # Needed to modify global copy of read
read = True # next time read==True case will be executed
sql = """ INSERT INTO Keluar(Nama, No_ID, datetime) VALUES ('Dhe$
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
查看代码访问和修改全局变量
global count
def modi():
global count # required to modify the global copy
count=10
def printc():
if (count==10): # read it as a normal variable
print("yes")
modi()
printc()