简体   繁体   English

知道我在 python 上的代码有什么问题吗?

[英]Any idea what's wrong with my code on python?

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每次我运行这段代码时,它只打印 def base() 中所有的 ada_dosen 函数,如果我用 ctrl+c 停止它,这里是回溯

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?任何ide有什么问题所以我的def base()上的所有功能都在运行?

There are some issues in your code, to highlight a few -您的代码中存在一些问题,突出显示一些 -

  1. 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.您的所有函数都定义为def <function name>() ,您没有结尾: ,否则您很可能会遇到语法错误。

  2. 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):您已将base函数定义为def base() ,但在程序的主要部分,您试图通过将参数nomor作为base(nomor)传递来调用它,我认为您需要更改函数定义以接受参数为 - def base(nomor):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM