4
\$\begingroup\$

This is a simple guessing game where you are prompted for a minimum and maximum value, then you must guess the number the computer chooses! This is the first program I have written so any and all suggestions are welcome.

def guessinggame():
    print "Welcome to the Guessing Game!."
    while True:
        try:
            numrange1 = int(raw_input("What is the minimum number you want?: "))
            break
        except ValueError:
            print "This must be an integer!"
    while True:
        try:
            numrange2 = int(raw_input("What is the maximum number you want?: "))
            break
        except ValueError:
            print "This must be an integer!"
    rnumber = random.randint(numrange1, numrange2)
    i = 0
    while True:
        x = int(raw_input("What is your guess?: "))
        if rnumber > x:
            print "Low!"
            i = i + 1
        elif rnumber < x:
            print "High!"
            i = i + 1
        elif rnumber == x:
            print "Correct!"
            i = i + 1
            print i
            break
    again = raw_input("Would you like to play again? (y/n): ")
    if again == 'y':
        guessinggame()
guessinggame()
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You are inappropriately using the guessinggame() function as if it were a goto label. After playing the game repeatedly, you will end up with a deep stack trace (which you can see by hitting ControlC), which is a bad sign. A proper design would be:

def guessing_game():
    # Code to play one game

play = True
while play:
    guessing_game()
    play = ('y' == raw_input('Would you like to play again? (y/n): '))

Curiously, you check that the bounds are integers, but you don't bother to validate the guesses. You also don't check that the minimum is less than the maximum.

All three prompts would benefit from a shared integer-prompting routine:

def prompt_int(prompt, err_msg='This must be an integer!'):
    while True:
        try:
            return int(raw_input(prompt))
        except ValueError:
            print err_msg

Getting that clutter out of the way makes your main function more readable:

def guessing_game():
    print "Welcome to the Guessing Game!."

    rnumber = None
    while rnumber is None:
        lower = prompt_int('What is the minimum number you want?: ')
        upper = prompt_int('What is the maximum number you want?: ')
        if lower < upper:
            rnumber = random.randint(lower, upper)
        else:
            print "The minimum must be less than the maximum!"

    tries = 0
    while True:
        guess = prompt_int("What is your guess?: ")
        tries += 1
        if guess < rnumber:
            print "Low!"
        elif guess > rnumber:
            print "High!"
        elif guess == rnumber:
            print "Correct!"
            print tries
            break

I've renamed numrange1 to lower, numrange2 to upper, i to tries, and x to guess. Incrementing the loop counter is common to all three branches and can be factored out.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.