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 Number Game Takeaways

Sachin Kanchan
Sachin Kanchan
564 Points

Reverse Number Game: I have written a program, works for me, but I would like to hear your feedback.

I have written this program. The computer asks YOU to enter a number between 1 & 20 that it will try to guess. Then it will compare your guess with random generated number. If it matches it will display result.

Else it will give a number and ask for feedback.

If you enter "l" means your number is lower than what it is displayed, and it will generate random numbers between displayed number and lower limit (1).

If you enter "h" means your number is higher than what it is displayed, and it will generate random numbers between displayed number and higher limit (20).

But there is an issue, If my secret_num is 10, and it generates 13, I'll enter "l", then it will generate something lower than 13. Say it generates 7, and asks if my number is higher than 7. If I enter "h" it jumps again to numbers more than 13. Can you help me make it so that the new generated numbers are between initial guess and current guess, i.e, 7 & 13.

import random

def game():

    # generate random number between 1 & 20
    secret_num = int(input("Enter a number between 1 & 20 for me to guess: "))

    # computer guesses numbers to display to user    
    guess = random.randint(1, 20)            
    guesses = []

    while len(guesses) < 5:

        if guess==secret_num:
            print("Ya'ay! I got it. The number is {}".format(secret_num))
            break

        elif guess!=secret_num:

            # if it doesn't get correct answer in 1st attempt, it will ask for feedback
            # type "l" if your secret_num is less than computer's guess
            # type "h" if your secret_num is more than computer's guess

            print("Is it lower than or higher than {}".format(guess))
            user_input= input("")
            if user_input=="l":
                guess = random.randint(1, guess)
                print(guess)
            elif user_input=="h":
                guess = random.randint(guess, 20)
                print(guess)
        guesses.append(guess)

    else:
        print("I lost. Your number was: {}".format(secret_num))
game()

Thanks

Sachin Kanchan
Sachin Kanchan
564 Points

Yuda Leh Hi Yuda. I saw your attempt to the issue I am facing now. https://teamtreehouse.com/community/my-inverse-of-number-game

I would appreciate your help.

Why did you use the "range(10)" keyword?

Also can you please explain me the below part, where you tried to ensure that a number isn't repeated twice.

            l1 = [i for i in guess_range if i > computer_num]
            l2 = [i for i in l1 if i not in exclude_list]
            computer_num = random.choice(l2)
        else:             
            exclude_list.append(computer_num)
            print("Computer guessed {}, a higher number than yours.".format(computer_num))
            l3 = [i for i in guess_range if i < computer_num]
            l4 = [i for i in l3 if i not in exclude_list]
            computer_num = random.choice(l4)

I am confused especially about the "i for i" part. Also I see that your code results in the computers win always and that would feel like cheating against a human user. How many guesses does your code allow?

Sincerely Sachin