Python Forum
'Time Limit Exceeded' Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Time Limit Exceeded' Problem
#1
I'm trying to run this code & keep getting a 'Time Limit Exceeded' Error when trying to run it.
Could it be the online IDE that I am using, or are the instructions in my code more complex than it should be?
I'm currently still getting my feet wet with Programming and only have a little bit over a week of experience
under my belt. Any and all advice is welcome.

import random

# Automated Number Guessing Game.
# Commented out code and edited it so it doesn't take user input
# And instead generates a random number to guess automatically

def startGame():
    print("Welcome to the Game")
    #name = input('Please enter your name: ')
    #print('Hello ' + name, end='. ')
    print("I am thinking of a number between 1 - 10")

    secretnumber = random.randint(1, 9)

    for i in range(3, -1, -1):
        print("Guess The Number: ")
        guess = random.randint(1, 9)
        if guess < secretnumber and i > 0:
            print("Your Guess: " + str(guess))
            print(str(i) + ' Guesses left.')
            print("Guess Higher")
        elif guess > secretnumber and i > 0:
            print("Your Guess: " + str(guess))
            print(str(i) + ' Guesses left.')
            print("Guess Lower")
        else:
            break

    if guess == secretnumber:
        print("Your Guess: " + str(guess))
        print("You got it Correct")
    else:
        print("Your Guess: " + str(guess))
        print('Aw, your out of Guesses')
        #print('Aw ' + name + ', your out of Guesses.')


startGame()
Reply
#2
The online IDE would likely be the problem. Have you tried running it in IDLE?

As for advice...

Line 15, change to range(3, 0, -1) and you can remove the "i > 0" tests on lines 18 and 22. The loop will automatically end when i == 0.

Instead of concatenating your strings on lines 19, 20, 24, 25, 30, and 33, use string formatting instead. F-strings or the str.format() method can work wonders and make your code more readable.

Don't Repeat Yourself (DRY). Lines 19, 20, 30, and 33 are identical. It would be better to reorganize a little to reduce the duplicates. Less duplication means less opportunity for bugs to creep in.

def startGame():
    print("Welcome to the Game")
    print("I am thinking of a number between 1 - 10")
 
    secretnumber = random.randint(1, 9)
 
    for i in range(3, 0, -1):
        print("Guess The Number: ")
        guess = random.randint(1, 9)
        print("Your Guess: " + str(guess))
        print(str(i) + ' Guesses left.')

        if guess < secretnumber:
            print("Guess Higher")
        elif guess > secretnumber:
            print("Guess Lower")
        else:
            break
 
    if guess == secretnumber:
        print("You got it Correct")
    else:
        print('Aw, your out of Guesses')
Reply
#3
No I haven't ran it on an IDLE yet, it's just something small I wrote on the iPad to keep my memory
fresh. I just got home and it works though. & Yeah I see looking back I used the range wrong. Appreciate
the help and thanks for the quick reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 565 Nov-10-2023, 08:19 PM
Last Post: snippsat
  Problem with module time and leap seconds Pedroski55 3 1,242 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Assign a value if datetime is in between a particular time limit klllmmm 2 2,758 Jan-02-2021, 07:00 AM
Last Post: klllmmm
  Time Limit Exceeded error loves 5 3,148 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,753 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  RecursionError: maximum recursion depth exceeded in comparison ? leoahum 11 13,059 Mar-18-2019, 01:53 PM
Last Post: leoahum
  time.sleep(...) problem DPaul 3 2,794 Jan-28-2019, 11:40 AM
Last Post: Larz60+
  fibonacci ***Time limit exceeded*** frequency 18 10,207 Nov-29-2018, 09:03 PM
Last Post: frequency
  If conditions with time limit unknowntothem 4 3,065 Nov-09-2018, 08:59 PM
Last Post: nilamo
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 3,804 Sep-12-2018, 01:08 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020