Python: 忽略用户的输入

Python: ignoring input from users

这是我添加到聊天信使中的数学游戏。每 60 秒问一道数学题。在提出新问题之前,用户有 60 秒的时间回答问题。

def askQuestion() 提供问题。

def checkAnswer(msg, name) 检查用户输入的消息是否是问题的正确答案,然后说出谁得到了正确答案。

问题是不止一位用户能够回答问题。我怎样才能做到这一点,一旦其中一位用户给出了正确答案,任何其他给出正确答案的用户都会被忽略?

我是编码新手,正在使用 Python 2.7.

def askQuestion():
    if question == 'add':
        a = randrange(100, 999)
        b = randrange(10, 99)
        question = 'What is ' + str(a) + ' + ' + str(b) + '?'
        correctAnswer = str(a + b)
    elif question == 'multiply':
        a = randrange(100, 999)
        availableB = [0, 1, 10, 5]
        b = availableB[randrange(4)]
        question = 'What is ' + str(a) + ' x ' + str(b) + '?'
        correctAnswer = str(a * b)
    print(question)

Timer(60 * 1000, askQuestion, repeat=True, timeType='real')

def checkAnswer(msg, name):
    if msg.lower() == correctAnswer:
        print(name + ' got the correct answer!')

最好的解决方案可能不是修改 checkAnswer,而是修改当用户发布消息时调用 checkAnswer 的代码部分。

但是,您没有提供那部分代码。

一种可能的解决方案是向 checkAnswer 添加一个“状态”变量,并在调用之间保留一个持久值;此变量将告诉 checkAnswer 当前是否接受答案。

我将调用这个变量checkAnswer.alreadyAnswered;每次提出新问题时设置为False,每次给出正确答案时设置为True

def checkAnswer(msg, name):
    if not checkAnswer.alreadyAnswered:
        if msg.lower() == correctAnswer:
            checkAnswer.alreadyAnswered = True
            Internal._chatMessage(name + " got the correct answer!")
checkAnswer.alreadyAnswered = False

def askQuestion():
    if question == 'add':
        a = randrange(100, 999)
        b = randrange(10, 99)
        question = 'What is ' + str(a) + ' + ' + str(b) + '?'
        correctAnswer = str(a + b)
    elif question == 'multiply':
        a = randrange(100, 999)
        availableB = [0, 1, 10, 5]
        b = availableB[randrange(4)]
        question = 'What is ' + str(a) + ' x ' + str(b) + '?'
        correctAnswer = str(a * b)
    checkAnswer.alreadyAnswered = False
    Internal._chatMessage(question)

Timer(60 * 1000, askQuestion, repeat=True, timeType='real')

像这样使用超时:

 
 
# Event object used to send signals from one thread to another
stop_event = Event()
 
 
def do_actions():
    i = 0
    while True:
        i += 1
        Timer(60 * 1000, askQuestion, repeat=True, timeType='real')
 
        # Here we make the check if the other thread sent a signal to stop execution.
        if stop_event.is_set():
            break
 
 
if __name__ == '__main__':
    # We create another Thread
    action_thread = Thread(target=do_actions)
 
    # Here we start the thread and we wait 5 seconds before the code continues to execute.
    action_thread.start()
    action_thread.join(timeout=5)
 
    # We send a signal that the other thread should stop.
    stop_event.set()

这行得通!谢谢大家的帮助!

correctAnswer = None
answeredBy = None

def askQuestion():
    global answeredBy
    global correctAnswer
    if question == 'add':
        a = randrange(100, 999)
        b = randrange(10, 99)
        question = 'What is ' + str(a) + ' + ' + str(b) + '?'
        correctAnswer = str(a + b)
    elif question == 'multiply':
        a = randrange(100, 999)
        availableB = [0, 1, 10, 5]
        b = availableB[randrange(4)]
        question = 'What is ' + str(a) + ' x ' + str(b) + '?'
        correctAnswer = str(a * b)
    print(question)
    answeredBy = None
    return

Timer(60 * 1000, askQuestion, repeat=True, timeType='real')

def checkAnswer(msg, name):
    if answeredBy is not None:
        print(answeredBy + ' already got the correct answer!')
        return
    if msg.lower() == correctAnswer:
        print(name + ' got the correct answer!')
        return