Random Number Guessing Game

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Random Number Guessing Game

Postby Laurenlena » Sat Sep 24, 2016 9:21 pm

I'm working on a problem for introduction to programming. I'm supposed to make a game that uses a random integer. The user enters guesses. The program displays if it is too high or too low. When the user guesses correctly, the program congratulates the user and asks if the user would like to play again. It also displays how many guesses it took. I've written the program and it works . . . sometimes. It seems to randomly terminate without any explanation. There is no error code or anything, it just ends in the middle of giving guesses. Other times it works fine and keeps going through the game. Can anyone tell me what I've written that is causing my game it quit unexpectedly?

Code: Select all

import random

def main():
    count = 1
    guess = get_guess()
    number = get_number()
   
    while guess > number:
        print('Too high, try again.')
        guess = int(input('Guess again!: '))
        count = count + 1
       
    while guess < number:
        print('Too low, try again.')
        guess = int(input('Guess again!: '))
        count = count + 1
       
    while guess == number:
        print('Good job!')
        print('It took you', count, 'guesses to get the answer.')
        again = input('Do you want to play again? y or n: ')

        while again == 'y':
            main()
        else:
            break

def get_guess():
    guess = int(input("I'm thinking of a number between 1 & 100. \
    What is it? "))
    return guess

def get_number():
    number = random.randint(1, 100)
    return number

main()
Last edited by Yoriz on Sat Sep 24, 2016 9:46 pm, edited 1 time in total.
Reason: First post lock.
Laurenlena
 
Posts: 2
Joined: Sat Sep 24, 2016 9:12 pm

Re: Random Number Guessing Game

Postby ichabod801 » Sun Sep 25, 2016 3:01 am

Here's what I expect is happening: Say your first guess is low. It skips over the first while loop and goes into the second. Then you guess some more, but one of your later guesses is too high. So it leaves the second while loop, but doesn't go into the third one. That means it exits the main function without asking if they want to play again.

Now note that you only want the third while loop to execute once. If you just want something to execute once, you want an if clause, not a loop.

What you want is one while loop for the game. Then you want to get the guess and use if/elif to check if the guess is high or low. If it's right, you can exit either with an else clause and break, or with the condition of the while loop.

Then you put that while loop into another while loop that continues as long as they want to play again.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Random Number Guessing Game

Postby Laurenlena » Sun Sep 25, 2016 3:29 am

Yes! That was it. Thank you so much. I never would've thought of that.

For reference, here's what the program looks like now.

Code: Select all
import random



def main():
    count = 1
    guess = get_guess()
    number = get_number()

    while guess != number:

        if guess > number:
            print('Too high, try again.')
            guess = int(input('Guess again!: '))
            count = count + 1

        if guess < number:
            print('Too low, try again.')
            guess = int(input('Guess again!: '))
            count = count + 1

    if guess == number:
        print('Good job!')
        print('It took you', count, 'guesses to get the answer.')
        again = input('Do you want to play again? y or n ')
        while again == 'y' or again == 'Y':
            main()
        else:
            print('Goodbye')


def get_guess():
    guess = int(input("I'm thinking of a number between 1 & 100. \
    What is it? "))
    return guess

def get_number():
    number = random.randint(1, 100)
    return number

main()
Laurenlena
 
Posts: 2
Joined: Sat Sep 24, 2016 9:12 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 3 guests