Python Forum

Full Version: 'Time Limit Exceeded' Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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')
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.