2017-03-19 14 views
1

私はタイマーと数学の質問という2つのプログラムを実行したいと思います。しかし、常に入力はタイマー機能を停止しているか、まったく実行していないようです。それを回避する方法はありますか? 例を単純にしておきます。制限時間付きの数学クイズ(同時関数) - 高度なPython

import time 

start_time = time.time() 
timer=0 
correct = answer 
answer = input("9 + 9 = ") 
#technically a math question here 
#so here until i enter the input prevents computer reading the code 
while True: 
    timer = time.time() - start_time 
    if timer > 3: 
#3 seconds is the limit 
    print('Wrong!') 
quit() 

要約3秒以内に質問に回答したいと思います。

3秒後にゲームが間違って終了

タイマーは「終了」または「間違った」がトリガする前に停止し、

を終了することになる3秒以内にプレイヤーの答えはあなたが理解してほしい場合は印刷されます、そして本当にあなたが msvcrtモジュールの kbhitgetch機能を使用することができますWindowsでは、あなたの助け

+2

あなたは全く研究をしましたか?例:http://stackoverflow.com/q/1335507/3001761 – jonrsharpe

+0

私はPythonプログラマーではありませんが、これには並行性/複数のスレッドが必要と思われます。 – ostrichofevil

+0

Windows、Linuxなどのオペレーティングシステムを使用していますか? – skrx

答えて

0

に感謝(私は近代化このcode example少し):

import sys 
import time 
import msvcrt 


def read_input(caption, timeout=5): 
    start_time = time.time() 
    print(caption) 
    inpt = '' 
    while True: 
     if msvcrt.kbhit(): # Check if a key press is waiting. 
      # Check which key was pressed and turn it into a unicode string. 
      char = msvcrt.getche().decode(encoding='utf-8') 
      # If enter was pressed, return the inpt. 
      if char in ('\n', '\r'): # enter key 
       return inpt 
      # If another key was pressed, concatenate with previous chars. 
      elif char >= ' ': # Keys greater or equal to space key. 
       inpt += char 
     # If time is up, return the inpt. 
     if time.time()-start_time > timeout: 
      print('\nTime is up.') 
      return inpt 

# and some examples of usage 
ans = read_input('Please type a name', timeout=4) 
print('The name is {}'.format(ans)) 
ans = read_input('Please enter a number', timeout=3) 
print('The number is {}'.format(ans)) 

他のオペレーティングシステムで正確に何をしなければならないかはわかりません(調査termios、tty、select)。

別の可能性は、同様にgetchは機能を持っており、あなたは(非ブロッキング)nodelay(1)にそれを設定することができますが、Windowsのためにあなたが最初Christopher Gohlke's websiteから呪いをダウンロードする必要がcursesモジュールになります。

import time 
import curses 


def main(stdscr): 
    curses.noecho() # Now curses doesn't display the pressed key anymore. 
    stdscr.nodelay(1) # Makes the `getch` method non-blocking. 
    stdscr.scrollok(True) # When bottom of screen is reached scroll the window. 
    # We use `addstr` instead of `print`. 
    stdscr.addstr('Press "q" to exit...\n') 
    # Tuples of question and answer. 
    question_list = [('4 + 5 = ', '9'), ('7 - 4 = ', '3')] 
    question_index = 0 
    # Unpack the first question-answer tuple. 
    question, correct_answer = question_list[question_index] 
    stdscr.addstr(question) # Display the question. 

    answer = '' # Here we store the current answer of the user. 
    # A set of numbers to check if the user has entered a number. 
    # We have to convert the number strings to ordinals, because 
    # that's what `getch` returns. 
    numbers = {ord(str(n)) for n in range(10)} 

    start_time = time.time() # Start the timer. 

    while True: 
     timer = time.time() - start_time 

     inpt = stdscr.getch() # Here we get the pressed key. 
     if inpt == ord('q'): # 'q' quits the game. 
      break 
     if inpt in numbers: 
      answer += chr(inpt) 
      stdscr.addstr(chr(inpt), curses.A_BOLD) 
     if inpt in (ord('\n'), ord('\r')): # Enter pressed. 
      if answer == correct_answer: 
       stdscr.addstr('\nCorrect\n', curses.A_BOLD) 
      else: 
       stdscr.addstr('\nWrong\n', curses.A_BOLD) 

     if timer > 3: 
      stdscr.addstr('\nToo late. Next question.\n') 

     if timer > 3 or inpt in (ord('\n'), ord('\r')): 
      # Time is up or enter was pressed; reset and show next question. 
      answer = '' 
      start_time = time.time() # Reset the timer. 
      question_index += 1 
      # Keep question index in the correct range. 
      question_index %= len(question_list) 
      question, correct_answer = question_list[question_index] 
      stdscr.addstr(question) 

# We use wrapper to start the program. 
# It handles exceptions and resets the terminal after the game. 
curses.wrapper(main) 
+0

入力がなく、名前を入力してもコードが入力を無視して実行を続行します –

+0

IDLEやPyCharmのようなコマンドラインやIDEから実行しますか?あなたはWindowsを使っていますか? Windowsのコマンドラインを使用する必要があります。 – skrx

+0

@Yo LEEは動作しているかどうか教えてください。後でcursesを使用する例を追加します。 – skrx