Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Squared

Completed the extra question credit need to review.

Question: Try to build the opposite of this game. Make a game where the computer tries to guess your secret number. You'll need to come up with a way to tell the computer if it's too high, too low, or if it guesses the number!

Here is the answer.

import random

guess_num = random.randint(1, 10)
guessed_num = []

print("""Wellcome to the guessing number game. Guess a number.
         type 'high' the computer guess is high.
         type 'low' if the computer guess is low
         type 'yes' if the computer guess is correct""")

while len(guessed_num) < 5:
    print("My number is {}".format(guess_num))
    secret_num = input("Am i right? ")
    if guess_num not in guessed_num:
        if secret_num == 'high':
            guess_num = random.randint(guess_num + 1, 10)

        elif secret_num == 'low':
            guess_num = random.randint(1, guess_num - 1)
        elif secret_num == 'yes':
            print("Yes I win!!!")

1 Answer

A couple of things -> it would be nice if you mention the range of numbers that a player can guess. It would be nice to allow the human to think of a number before the computer begins guessing. If you want your program to efficiently guess the number it should start with 5 every time. Currently when the computer guess too high the human must type "low" and vice versa which seems backwards. It would be good to add try and except blocks to the program incase of wrong input on the part of the user. Currently if the user enters wrong input the program doesn't break but it stops working correctly.