如何通过倒计时停止显示输入用户猜测

时间:2013-12-30 19:32:27

标签: python

我正在编写自己的关于心灵操作的游戏,例如加法,减法,乘法和除法。现在,游戏在加法和减法操作中运行顺畅,但我现在想要的是向用户询问他/她的猜测但是如果他/她延迟5秒输入消息应该消失,则应该出现正确的结果和另一个操作应该出现。这是游戏的以下代码:

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
        #====Repeat operations 5 times=================
    for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
        easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
        for item in easy_level:
            numbers_with_operators.append(item)
            time.sleep(1)
            numbers_with_operators.append(local_operators_easy[randint(0,1)])
            if len(numbers_with_operators)==18:
                numbers_with_operators.append(randint(1,10))
            print numbers_with_operators
        time_for_guess=time.time()
        deadline_for_guess=time_for_guess+5
        while time_for_guess<deadline_for_guess:
            user_guess=int(raw_input("What is the result? "))
            break
        computer_result=compute_list(numbers_with_operators)
        if user_guess==computer_result:
            goodGuess+=1
            print "Good guess!"
        else:
            print "Sorry, that is not the result"
            badGuess+=1
            print computer_result
        del numbers_with_operators[:]
    duration=default_timer()-time_easy_level
    continuePlaying=False
    print "Your results are: \n"
    print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
    print "Total seconds playing:\n"+str(duration)+" seconds"
    return continuePlaying

欢迎所有建议,并随时修改我的代码:)

3 个答案:

答案 0 :(得分:0)

N = 100 # you might need to adjust this
for i in range(N):
    print "\n"

然后将所有内容重新打印回屏幕,除了输入消息。

清除屏幕的另一种方法是:

import os
os.system('cls')

但是你需要检查你定义的变量是否仍然保留在此之后。

关于禁用raw_input功能,请检查此post

答案 1 :(得分:0)

请参阅this post

你当前的while循环不起作用;这不是真正正确使用它,它只是无休止地等待你给出一个输入。在你的情况下,我认为一个好的方法是定义一个函数来询问用户每次线程时递归调用的答案.Timer到期。一些示例代码:

import threading
def guess_a_number():
    #generate random numbers
    #generate operators
    timer = threading.Timer(5.0, guess_a_number())
    timer.start()
    #get user input
    #check if user input is correct
    if True:
        print "You were right!"
        guess_a_number()
    else:
        print "Sorry, you were wrong."
        guess_a_number()

我认为这应该会使计时器到期时或响应(无论是正确的还是不正确的)重新开始。具体情况显然取决于你。

编辑错字。

答案 2 :(得分:0)

感谢@aenda和@lennon310提出的建议并对以下帖子进行了一些研究:Non-blocking raw_input code from Gary RobinsonRaw input and timeoutKeyboard input with timeout in python我能够做一些细微的更改并生成以下代码解决了我的问题。

timeout=7

class AlarmException(Exception):
    pass

def alarmHandler(signum,frame):
    raise AlarmException

def my_raw_input(prompt,timeout):
    signal.signal(signal.SIGALRM,alarmHandler)
    signal.alarm(timeout)
    try:
        userGuess=int(raw_input(prompt))
        signal.alarm(0)
        return userGuess
    except AlarmException:
        print "Uh-oh! Time's for that one!"
    signal.signal(signal.SIGALRM,signal.SIG_IGN)
    return ''

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
    global timeout
    #====Repeat operations 5 times=================
        for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
            easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
            for item in easy_level:
                numbers_with_operators.append(item)
                time.sleep(1)
                numbers_with_operators.append(local_operators_easy[randint(0,1)])
                    if len(numbers_with_operators)==18:
                        numbers_with_operators.append(randint(1,10))
                print numbers_with_operators
                user_guess = my_raw_input("What's the result? ",timeout)
                computer_result=compute_list(numbers_with_operators)
                if user_guess==computer_result:
                    goodGuess+=1
                    timeout=7
                    print "Good guess!"
                else:
                    print "Sorry, that is not the result"
                    badGuess+=1
                    timeout=7
                    print computer_result
              print "That was the number operation "+str(x)
              del numbers_with_operators[:]
        duration=default_timer()-time_easy_level
        continuePlaying=False
        print "Your results are: \n"
        print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
        print "Total seconds playing:\n"+str(duration)+" seconds"
        return continuePlaying