并行时间功能和非阻塞输入

问题描述

我不会用python编写代码但我必须用python编写一个简单的游戏,过程是程序生成2个随机数并询问用户这两个数字的总和是多少,用户有5秒的时间来回答如果超过 5 秒它应该说超时并显示点,否则如果用户在 5 秒内正确回答它应该终止或终止时间功能,制作其他随机数并询问它们的总和

输入函数阻塞程序并等待输入,所以我做了两个进程,我不确定我做对了还是不对,它仍然不能并行工作,看起来睡眠函数也阻塞了程序

我在 python 中搜索了很多关于并行编程和非阻塞输入的内容,但我无法修复它


import random
import time
from multiprocessing import Process

point = int(0)

check = bool(False)

def runInParallel(*fns):
  proc = []
  for fn in fns:
    p = Process(target=fn)
    p.start()
    proc.append(p)
  for p in proc:
    p.join()


def timeingFunc():
    timer = int(3)
    time.sleep(timer)
    print("Time Out")
    print(point)



def logic():
    counter = 0
    +(+counter)
    x = random.getrandbits(2)
    y = random.getrandbits(2)

    print("%3f + %2f = " % (x,y))
    result = int(input())




    if result == int (x + y):
       +(+point)
       print("Good")
       check = True






if __name__ == '__main__':
    while 1>0:
        runInParallel(logic(),timeingFunc())
   


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)