我有连接到覆盆子pi的usb RFID阅读器 RFID阅读器是中国品牌,它像键盘一样,它读取前10位数
所以我在这里尝试做的是从卡片中读取并与我的代码中存储的数字进行比较
#!/usr/bin/env python
import time
import sys
card = '0019171125' # Stored good card number consider using a list or a file.
def main(): # define a main function.
while True: # loop until the program encounters an error.
sys.stdin = open('/dev/tty0', 'r')
RFID_input = input()
if RFID_input == card: # coppare the stored number to the input and if True execute code.
print "Access Granted"
print "Read code from RFID reader:{0}".format(RFID_input)
else: # and if the condition is false excecute this code.
print "Access Denied"
tty.close()
main() # call the main function.
但是这个错误一直显示
RFID_input = input()
^ IndentationError:unindent与任何外部缩进级别都不匹配
任何建议
答案 0 :(得分:7)
Python是缩进敏感的,因此您需要正确缩进代码:
#!/usr/bin/env python
import time
import sys
card = '0019171125'
def main():
while True:
sys.stdin = open('/dev/tty0', 'r')
RFID_input = input()
if RFID_input == card:
print "Access Granted"
print "Read code from RFID reader:{0}".format(RFID_input)
else:
print "Access Denied"
tty.close()
main()
请注意tty.close()
会引发错误,因为没有定义tty
。您可能希望在那里关闭sys.stdin
,但是当您可以直接从该流中读取时,将sys.stdin
用于其他流绝对不是一个好主意。
此外,请勿使用input()
进行用户输入,请使用raw_input
。
def main():
with open('/dev/tty0', 'r') as tty:
while True:
RFID_input = tty.readline()
if RFID_input == card:
print "Access Granted"
print "Read code from RFID reader:{0}".format(RFID_input)
else:
print "Access Denied"
答案 1 :(得分:1)
正确的缩进:
#!/usr/bin/env python
import time
import sys
card = '0019171125' # Stored good card number consider using a list or a file.
def main(): # define a main function.
while True: # loop until the program encounters an error.
sys.stdin = open('/dev/tty0', 'r')
RFID_input = input()
if RFID_input == card: # compare the stored number to the input and if True execute code.
print "Access Granted"
print "Read code from RFID reader:{0}".format(RFID_input)
else: # and if the condition is false excecute this code.
print "Access Denied"
# where is tty defined??
tty.close()
if __name__ == '__main__':
main()
但是你还没有定义......