在计时器仍在运行时,如何使输入询问密码?
我正在尝试建立出勤系统,现在,我想创建一个随机密码,并将其放在倒计时中,以便随着它的用完,学生将无法再使用代码了。但是,当我尝试运行它时,它仅显示密码和倒计时,仅在计时器用完后才要求输入。
我试图使用循环以及多处理模块,但无济于事。我怀疑该错误位于我对线程使用的某个地方。
import threading
#create code and timer
Thread1 = threading.Thread(target=generateCodeandTimer(600))
# make input
Thread2 = threading.Thread(target=attend)
# Start the thread
Thread1.start()
# Start the thread
Thread2.start()
但是作为参考,这是我的完整代码:
import string
import random
import time
import sys
import threading
code = ""
def generateCodeandTimer(s):
global code
code = ''.join((random.choice(string.ascii_lowercase + string.digits) for x in range(6)))
print("Attendance code:", code)
while s != -1:
mins = s // 60
secs = s % 60
countdown = '{:02d}:{:02d}'.format(mins, secs)
sys.stdout.write('\r' + countdown)
time.sleep(1)
s -= 1
if s==-1:
print()
print("Code expired")
def attend():
print()
studentinput = input("Please enter the code")
if studentinput == code:
print()
print("Your attendance has been taken")
else:
print()
print("Wrong code!")
#create code and timer
Thread1 = threading.Thread(target=generateCodeandTimer(600))
# make input
Thread2 = threading.Thread(target=attend)
# Start the thread
Thread1.start()
# Start the thread
Thread2.start()
I am trying to make an attendance system and right now, I want to create a random password, and put it on a countdown so that as it runs out, the student can't use the code anymore. However, when I try to run it, it only displays the password and the countdown, and only asks for input after the timer runs out.
I have attempted to use a for loop as well as the multiprocessing module to no avail. I suspect that the error is located somewhere around my use of the threads.
import threading
#create code and timer
Thread1 = threading.Thread(target=generateCodeandTimer(600))
# make input
Thread2 = threading.Thread(target=attend)
# Start the thread
Thread1.start()
# Start the thread
Thread2.start()
But for reference, this is my full code:
import string
import random
import time
import sys
import threading
code = ""
def generateCodeandTimer(s):
global code
code = ''.join((random.choice(string.ascii_lowercase + string.digits) for x in range(6)))
print("Attendance code:", code)
while s != -1:
mins = s // 60
secs = s % 60
countdown = '{:02d}:{:02d}'.format(mins, secs)
sys.stdout.write('\r' + countdown)
time.sleep(1)
s -= 1
if s==-1:
print()
print("Code expired")
def attend():
print()
studentinput = input("Please enter the code")
if studentinput == code:
print()
print("Your attendance has been taken")
else:
print()
print("Wrong code!")
#create code and timer
Thread1 = threading.Thread(target=generateCodeandTimer(600))
# make input
Thread2 = threading.Thread(target=attend)
# Start the thread
Thread1.start()
# Start the thread
Thread2.start()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这一行中:
您实际上是调用函数generateCodeAndtimer。目标关键字需要一个函数对象,但是此代码调用该功能,然后将结果作为线程的目标传递。
第二次启动线程时,您就正确了:
请注意差异:
target =参加
通过功能对象参加
,因为您不调用该功能。如果您已经写了target = ather =()
,则将调用该功能并将其结果作为目标。该解决方案在线程构造器的文档中找到。将第一个线程创建更改为:
需要600之后的逗号,因为args =关键字需要一个元组。
现在,您的程序将按照您的意图运行。您会发现其他一些问题 - 例如,当用户输入密码中时,程序将不会立即退出。但是,如果您遇到麻烦,我会让您弄清楚这些,或者提出更多问题。
In this line:
you are actually calling the function generateCodeandTimer. The target keyword requires a function object, but this code calls the function and then passes the result as the target of the thread.
The second time you started a thread, you got it right:
Note the difference:
target=attend
passes the function objectattend
because you do not CALL the function. If you had writtentarget=attend()
, you would have called the function and passed its result as the target.The solution is found in the documentation for the Thread constructor. Change the first thread creation to this:
The comma after 600 is necessary because the args= keyword requires a tuple.
Your program will now run as you intend. You will discover some other problems - for example, the program won't exit immediately when the user types in the password. But I will let you figure those out, or ask more questions if you run into trouble.