在计时器仍在运行时,如何使输入询问密码?

发布于 2025-02-10 08:03:11 字数 1424 浏览 2 评论 0原文

我正在尝试建立出勤系统,现在,我想创建一个随机密码,并将其放在倒计时中,以便随着它的用完,学生将无法再使用代码了。但是,当我尝试运行它时,它仅显示密码和倒计时,仅在计时器用完后才要求输入。

我试图使用循环以及多处理模块,但无济于事。我怀疑该错误位于我对线程使用的某个地方。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2025-02-17 08:03:11

在这一行中:

 Thread1 = threading.Thread(target=generateCodeandTimer(600))

您实际上是调用函数generateCodeAndtimer。目标关键字需要一个函数对象,但是此代码调用该功能,然后将结果作为线程的目标传递。

第二次启动线程时,您就正确了:

Thread2 = threading.Thread(target=attend) 

请注意差异:target =参加通过功能对象参加,因为您不调用该功能。如果您已经写了target = ather =(),则将调用该功能并将其结果作为目标。

该解决方案在线程构造器的文档中找到。将第一个线程创建更改为:

Thread1 = threading.Thread(target=generateCodeandTimer, args=(600,))

需要600之后的逗号,因为args =关键字需要一个元组。

现在,您的程序将按照您的意图运行。您会发现其他一些问题 - 例如,当用户输入密码中时,程序将不会立即退出。但是,如果您遇到麻烦,我会让您弄清楚这些,或者提出更多问题。

In this line:

 Thread1 = threading.Thread(target=generateCodeandTimer(600))

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:

Thread2 = threading.Thread(target=attend) 

Note the difference: target=attend passes the function object attend because you do not CALL the function. If you had written target=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:

Thread1 = threading.Thread(target=generateCodeandTimer, args=(600,))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文