Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:30:49+00:00 2026-06-16T00:30:49+00:00

I have a homework assignment to do and I really need a solution. I

  • 0

I have a homework assignment to do and I really need a solution. I have been trying to do this since yesterday but I do not know how.

Program has to generate and print a letter or a number and then a user has to type it as quickly as possible and press ENTER. The game is over after 30 secs.

Well I do not know how to put time limit to a game. I was searching through stackoverflow and I did not find anything useful. Please help me.

**Here it is what I have done so far. I tried code from the answer by SYSS.STDER, but it does not quite work because when the 30 secs are over, the game should also be over, but here in this code the game is over when I type last character.

LOOP WILL NOT STOP UNTIL IT FINISHES AND WE DISCOVER THAT WE ARE PAST OUR DEADLINE. THE TASK NEEDS TO BE INTERRUPTED IN PROGRESS AS SOON AS THE TIME ELAPSES.

max_time =30
start_time = time.time()  # remember when we started
while (time.time() - start_time) < max_time:

    response = "a"      # the variable that will hold the user's response
    c = "b"             #the variable that will hold the character the user should type
    score = 0
    number = 0

    c = random.choice(string.ascii_lowercase + string.digits)
    print(c)
    number = number + 1

    response = input("Type a letter or a number: ") #get the user's response

    if response == c and (time.time() - start_time) < max_time:
         # if the response from the previous loop matches the character
         # from the previous loop, increase the score.
         score = score + 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:30:49+00:00Added an answer on June 16, 2026 at 12:30 am

    Here’s my way to do it:

    import string
    import random 
    import time   
    
    response = "a"              # the variable that will hold the user's response
    c = "b"                     #the variable that will hold the character the user should type
    score = 0                   #the variable that will hold the user's score
    start = time.time()         #the variable that holds the starting time
    elapsed = 0                 #the variable that holds the number of seconds elapsed.
    while elapsed < 30:         #while less than 30 seconds have elapsed  
    
        if response == c:       #if the response from the previous loop matches the character
            score += 1          #from the previous loop, increase the score.
    
        #c is a random character
        c = random.choice(string.ascii_lowercase + string.digits)
        print(c)               
    
        response = input("Type a letter or a number: ") #get the user's response
    
        elapsed = time.time() - start #update the time elapsed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still not very good with data structures, but I have this homework assignment
I have been given a homework assignment where I need to create a captcha
I have to write this code for a homework assignment but I don't even
This is a homework assignment, I have to write a program that reads a
I'm trying to make a homework assignment where I have to use fork() but
I have a homework assignment where I need to take input from a file
I have a homework assignment to sort an array in ascending order. Obviously, this
Hi I have a homework assignment where I need to implement an intersection of
(this is indirectly a part of a much larger homework assignment) I have something
Good day, Stack Overflow. I have a homework assignment that I'm working on this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.