2016-01-30 90 views
1

編輯: 我通過實現@L3viathan's solution解決了這個問題。這裏是更新的代碼:如何限制用戶需要輸入的時間 - Python

import operator 
import random 
from time import time 
import sys 

def menu(): 
    menu = input("\n\n\n--------\n Menu \n--------\nPress:\n- (1) to play \n- (2) to exit\n: ") 
    if menu == "1": 
     play_game() 
    if menu == "2": 
     print("Exiting...") 
     sys.exit() 
    while menu != "1" or menu != "2": 
     print("Please enter a valid choice") 
     menu = input("--------\n Menu \n--------\nPress:\n- (1) to play \n- (2) to exit\n: ") 
     if menu == "1": 
      play_game() 
     if menu == "2": 
      print("Exiting...") 
      break 

def game_over(): 
    print("Game over.") 
    file = open("score.txt", "r") 
    highscore = file.read() 
    if int(highscore) < score: 
     file = open("score.txt", "w") 
     file.write(score) 
     file.close() 
     print("Score: {}\n\n******************\nNew highscore!\n******************".format(str(score))) 
    else: 
     print("Score: {}\nHighscore: {}".format(str(score), str(highscore))) 

def play_game(): 
    print("Type in the correct answer to the question\nYou have 3 seconds to answer each question\nThe game will continue until you answer a question incorrectly") #displays the welcome message 
    counter = 1 
    score = 0 

    while counter == 1: 
     ops = {"+":operator.add, 
       "-":operator.sub, 
       "x":operator.mul} 
     num1 = random.randint(0, 10) 
     op = random.choice(list(ops.keys())) 
     num2 = random.randint(1, 10) 
     print("\nWhat is {} {} {}? ".format(num1, op, num2)) 
     start = time() 
     guess = float(input("Enter your answer: ")) 
     stop = time() 
     answer = ops.get(op)(num1,num2) 

     if guess == answer: 
      if stop - start > 3: 
       print("You took too long to answer that question. (" + str(stop - start) + " seconds)") 
       def game_over(): 
        print("Game over.") 
        file = open("score.txt", "r") 
        highscore = file.read() 
        if int(highscore) < score: 
         file = open("score.txt", "w") 
         file.write(score) 
         file.close() 
         print("Score: {}\n\n******************\nNew highscore!\n******************".format(str(score))) 
        else: 
         print("Score: {}\nHighscore: {}".format(str(score), str(highscore))) 
        menu() 
       game_over() 
       break 
      else: 
       score = score + 1 
       print("Correct!\nScore: " + str(score)) 
     else: 
      print("Game over.") 
      counter = counter - 1 
      file = open("score.txt", "r") 
      highscore = file.read() 
      if int(highscore) < score: 
       file = open("score.txt", "w") 
       file.write(score) 
       file.close() 
       print("Score: {}\n\n******************\nNew highscore!\n******************".format(str(score))) 
      else: 
       print("Score: {}\nHighscore: {}".format(str(score), str(highscore))) 
     if counter != 1: 
      menu() 
menu() 

謝謝大家的貢獻。

------編輯結束-------

我一直在尋找堆棧溢出的解決方案,但是我找不到任何與我的遊戲作品,所以我appologise如果這是一個重複的問題。

我正在製作一個數學遊戲,用戶必須回答一個簡單的算術問題,每次用戶輸入正確的答案時,得分都會增加1。但是,如果用戶輸入錯誤的答案,則遊戲結束。

我想給遊戲添加超時功能,例如,當用戶輸入其中一個問題的答案時,如果用戶超過3秒鐘回答,遊戲結束。有誰知道如何做到這一點?

我能找到的所有解決方案都是針對Unix的,而不是Windows。

這裏是我的代碼:

import operator 
import random 

def play_game(): 
    print("Type in the correct answer to the question\nYou have 3 seconds to answer each question\nThe game will continue until you answer a question incorrectly") #displays the welcome message 
    counter = 1 
    score = 0 

    while counter == 1: 
     ops = {"+":operator.add, 
       "-":operator.sub, 
       "x":operator.mul} 
     num1 = random.randint(0, 10) 
     op = random.choice(list(ops.keys())) 
     num2 = random.randint(1, 10) 
     print("\nWhat is {} {} {}? ".format(num1, op, num2)) 
     guess = float(input("Enter your answer: ")) 
     answer = ops.get(op)(num1,num2) 

     if guess == answer: 
      score = score + 1 
      print("Correct!\nScore: " + str(score)) 
     else: 
      print("Game over.") 
      counter = counter - 1 
      file = open("score.txt", "r") 
      highscore = file.read() 
      if int(highscore) < score: 
       file = open("score.txt", "w") 
       file.write(score) 
       file.close() 
       print("Score: {}\n\n******************\nNew highscore!\n******************".format(str(score))) 
      else: 
       print("Score: {}\nHighscore: {}".format(str(score), str(highscore))) 
     if counter != 1: 
      menu = input("\n\n\nMenu\n----\nPress:\n- (1) to play again\n- (2) to exit\n: ") 
      if menu == "1": 
       play_game() 
      elif menu == "2": 
       print("Exiting...") 
       break 
      while menu != "1" or menu != "2": 
       print("Please enter a valid choice") 
       menu = input("Menu\n----\nPress:\n- (1) to play again\n- (2) to exit\n: ") 
       if menu == "1": 
        play_game() 
       elif menu == "2": 
        break 
        print("Exiting...") 
play_game() 
+1

什麼是錯誤?你有什麼嘗試?你有什麼問題? – johnharris85

+0

你想要的是一個線程 –

+0

@PadraicCunningham對不起? –

回答

0

做到這將是一次最簡單的方法調用input

from time import time 

start = time() 
answer = input("Answer this in 3 seconds: 1 + 1 = ") 
stop = time() 

if stop - start < 3: 
    if answer == "2": 
     print("Correct!") 
    else: 
     print("Wrong!") 
else: 
    print("Too slow") 

這種方式很簡單,但你不能放棄用戶在三秒後輸入的能力,只能等待它,然後看看它是否足夠快。

+0

不,他希望你只有3在「輸入」返回之前的幾秒鐘。 – Matt

+0

@Matt根據他們的編輯,他們似乎很滿意這個解決方案,但我也想到了這一點。我認爲這可能是不值得的努力,但如果你提交一個解決這個問題的答案,我會考慮更好的答案。 – L3viathan