Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python Basics - Solution: Number guessing game (opposite)

Hi, I have implemented a number guessing game where user will guess and computer will try to guess. I put the python script in a file number_game_computer.py.

Here is script in that file:

import random

def dialog(name, message):
    print("{}: {}".format(name, message))

def game():
    #define the player names
    user = "User"
    computer = "Computer"

    # generate a random number from 1 to 100 (that you will select for yourself at the beginning)
    valid_user_guess = False
    while not valid_user_guess:
        try:
            # get a number guess from the user
            user_guess = int(input("Guess a number between 1 and 100: "))
        except ValueError:
            print("Not a valid number.")
        else:
            if user_guess < 1 or user_guess > 100:
                print("Please guess number between 1 and 100 (inclusive)")
            else:
                valid_user_guess = True
                print("Enjoy the game!!!\n\n")

    # user throws the challenge to a computer
    dialog(user, "I have guessed a number between 1 and 100! Computer, it's your turn to guess. You can guess max 5 times")

    # Computer is boasting on it's own
    dialog(computer, "Don't challenge me. I am a machine.\n\n")

    guesses = []
    computer_guess_min = 1
    computer_guess_max = 100

    while len(guesses) < 5:
        computer_guess = random.randint(computer_guess_min, computer_guess_max)
        dialog(computer, "I guessed {}".format(computer_guess))

        guesses.append(computer_guess)

        # compare computer's guess with user's guess
        if computer_guess == user_guess:
            dialog(user, "You won this time. You took {} guesses".format(len(guesses)))
            break
        elif computer_guess < user_guess:
            dialog(user, "My guess is higher than {}".format(computer_guess))
            computer_guess_min = computer_guess + 1
        else:
            dialog(user, "My guess is lower than {}".format(computer_guess))
            computer_guess_max = computer_guess - 1
    else:
        dialog(user, "You didn't get it. My guess was {}. Indeed! You are just a machine.".format(user_guess))

    play_again = input("Do you want to play the game again (Y/n)? ")
    if play_again.lower() != 'n':
        game()
    else:
        print("Bye!")

game()

1 Answer

Thank you.