ihave this code
import serial
import time
import datetime
import MySQLdb as mdb
localtime = time.localtime(time.time())
port = serial.Serial("/dev/ttyUSB0", baudrate=9600)
count = 0
nomor =''
start = '\x02'
stop = '\x03'
def mulai():
print "Silahkan tempel kartu anda"
def no_dosen()
print "Dosen tidak terdaftar"
mulai()
def no_jadwal()
print "Tidak ada jadwal kuliah"
mulai()
def ada_dosen(dosen)
print dosen
return
def ada_matkul(matkul)
print matkul
return
def cek_dosen(no)
db = mdb.connect("localhost", "azis48", "azis48", "skripsi")
cur = db.cursor()
cond1 = "SELECT * FROM dosen WHERE kode_dosen = %s" %(no)
try:
cur.execute(cond1)
hitung = cur.rowcount
res1 = cur.fetchall()
for row in res1:
nama_dosen = row[1]
if hitung == 1:
return nama_dosen
elif hitung != 1:
return 'null'
except:
db.close()
def cek_jadwal(day,time)
db = mdb.connect("localhost", "azis48", "azis48", "skripsi")
cur = db.cursor()
cond2 = "SELECT nama_mk FROM jadwal WHERE hari = '%s' AND waktu = '%s'" %(day,time)
try:
cur.execute(cond2)
hitung = cur.rowcount
res2 = cur.fetchall()
for row in res2:
nama_mk = row[1]
if hitung == 1:
return nama_mk
elif hitung != 1:
return 'null'
except:
db.close()
def cek_pertemuan(matkul)
db = mdb.connect("localhost", "azis48", "azis48", "skripsi")
cur = db.cursor()
cond3 = "SELECT pertemuan_ke FROM acara WHERE nama_mk = '%s'" %(matkul)
try:
cur.execute(cond3)
res3 = cur.fetchall()
for row in res3:
pertemuan_ke = row[0]
return pertemuan_ke
except:
db.close()
def base()
day = localtime.tm_wday
time = localtime.tm_hour
no = str(nomor)
dosen = cek_dosen(no)
if dosen == 'null':
no_dosen()
elif dosen != 'null':
ada_dosen()
matkul = cek_jadwal(day,time)
if matkul == 'null':
no_jadwal()
elif matkul != 'null':
ada_matkul()
pertemuan = cek_pertemuan(matkul)
print pertemuan
mulai()
if __name__ == '__main__':
mulai()
while True:
data = port.read()
count += 1
if count == 1:
if str(data) != start:
nomor = ''
count = 0
elif 2 <= count <= 13:
nomor = nomor + str(data)
elif count == 16 and str(data) == stop:
base(nomor)
nomor = ''
count = 0
everytime i run this code, its only print the ada_dosen function from all in def base(), if i stop it with ctrl+c here is the traceback
DOSEN3
Traceback(most recent call last):
file "skripsi.py", line 111, in<module>
base(nomor)
any ide what wrong so all function on my def base() running?
There are some issues in your code, to highlight a few -
All your functions are defined as def <function name>()
, you do not have an ending :
, without that you should most probably be getting syntax errors.
You have defined your base
function as def base()
, but in your main part of the program, you are trying to call it by passing an argument nomor
as base(nomor)
, I think you need to change the function defnition to accept the argument as - def base(nomor):