1

I have decided to ask here because after spending several hours trying to figure out my issue (alone and online), I have not gotten anywhere.

Let me explain you my issue first:

I am trying to write a script that would randomly pick up a word from a predefined list, randomize the order of the letters from that word and show a picture with those randomized-order letters to the user. The role of the user is to input which is the first letter of the mystery word. My script is successful and working so far and I am pretty content with it. However, I have faced a considerable issue when I tried to implement a "time-limited" user input. In fact, I would like the user to have a limited amount of time to enter the first letter of the mystery word. If the user does not input anything, then the program would print something like "Time's up!" and would continue its course.

The closest I've been with the solution is with Alex Martelli's answer here: How to set time limit on raw_input However, the script doesn't seem to work for me. It definitely waits X min before exiting the loop but does not do anything else if I input something. I tried to use time.time() and store it into a variable and checking on each iteration, I tried different threading methods but no success. I've read plenty of posts and most of them could not help me because they were for linux or Mac (I'm on Windows).

I have put my current script below. Keep in mind that I only put the script for one option here for clarity sake, however there are 5 in the final version. cycle_nb is a variable counting the overall loop, if the user wants to do 5 questions in a row, then the whole thing would repeat 5 times, each time giving the user a defined amount of time to answer.

I have no clue about how to implement a time limited solution in my current configuration. I am no expert and I do not know an extensive amount of Python modules, I am mostly keeping up with this project to try and learn because I am very interested in programming. Therefore, I prefer to turn to actual experts who would have a much better idea than me to solve this, and hopefully learn from them in the process.

#First half of script that leads to production of an image (plt below) with randomized letters

plt.show()
answer = randomstring_answer
secret_word = randomword
player_answer = input()
option1 = randomstring

    if player_answer == "1":
        if option1 == answer:
            print("Correct!")
            print("The answer was " + secret_word)
            cycle_nb = cycle_nb+1
            plt.close()
        else:
            print("Wrong!")
            print("The answer was " + secret_word)
            cycle_nb = cycle_nb + 1
            plt.close()

#Rest of the script

Thank you in advance for your time and help!

Bilalord
  • 11
  • 1
  • Welcome to stackoverflow. Well crafted first question :) – Adrian Torrie Apr 24 '20 at 16:51
  • @AdrianTorrie Thank you very much! Considering it is a "well crafted" question, I can only take the absence of answers as a bit of reassurance for the reason why I am stuck with that issue for so long... :/ – Bilalord Apr 27 '20 at 07:18

0 Answers0