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

Gabor Kovacs
Gabor Kovacs
4,874 Points

Ask for help to my opposite game script

Hi! It's the main block of my script and I just need a little help. I don't know how to make it work so I would appreciate, if an expert give me a hand. Thanks in advance! :)

import random
my_num=int(input("My number: "))

def comp_guess():
    a=1
    b=10
    guess = int(random.randint(a,b))

    while True:
        if guess == my_num:
            print("You got it: ",my_num)
            break
        elif guess < my_num:
            print("Comp number: {}".format(guess))
            a+=1
            instruction = input("Smaller/Bigger: ")
            if instruction.lower() == "Bigger":
                comp_guess()
        elif guess > my_num:
            print("comp number: {}".format(guess))
            b+=1
            instruction = input("Smaller/Bigger: ")
            if instruction.lower() == "Smaller":
                comp_guess()

comp_guess()

[MOD: fix formatting replaced ''' with ``` -cf]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

I applaud the attempt at trying a recursive solution! It can sometime be difficult to clearly pass information from one recursive iteration to the next.

In each call to comp_guess(), the variables a and b are unique to each recursive call. Hence these values are not changing between calls. You may want to change the function signature to def comp_guess(a=1, b=10): then call the function with comp_guess(a, b).

The second issue is the instruction.lower() comparisons to "Bigger" and "Smaller". These will never match since a lowered string will never have the capitalized "B" or "S".

Post back with your updated code if you need more help. Good Luck!!!

Chris is definitely putting you on the right track. Also, when you program functionally and use recursion you won't need a loop.

Instead of printing the value, I would probably just return the number and pass that to a print statement.

You're really close, so don't give up.

Gabor Kovacs
Gabor Kovacs
4,874 Points

Hi! First of all thanks for the advices!:) So here is my updated main script. It's working but it's not perfect. Sometimes it guess the same number. I try to avoid that with(just make a print for that so it's looks more pretty) the list option, but it isn't working. Any suggestion? Thanks in advance!

import random

my_num=(input("My number: "))

a = 1
b = 10

def comp_guess(a,b):

    bad_guesses = []

    guess = str((random.randint(a,b)))

    if guess in bad_guesses:
        print("You already guess this number{}.Try again!".format(guess))
        comp_guess(a,b)  
    elif guess == my_num:
        print("You got it: ",my_num)
        print("Game Over")

    elif guess < my_num:
        a+=1
        bad_guesses.append(guess)
        print("Comp number: {}".format(guess))
        instruction = input("Smaller/Bigger: ")
        if instruction.lower() == "bigger":
            print(" ")
            comp_guess(a,b)
    elif guess > my_num:
        b-=1
        bad_guesses.append(guess)
        print("comp number: {}".format(guess))
        instruction = input("Smaller/Bigger: ")
        if instruction.lower() == "smaller":
            print(" ")
            comp_guess(a,b)


comp_guess(a,b)
Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

Like a and b, the list bad_guesses is local to each instance. It will also need to be passed to each recursive call.

Gabor Kovacs
Gabor Kovacs
4,874 Points

It works perfectly now! Thank you!

import random

my_num=int((input("My number: ")))

a = 1
b = 10
bad_guesses = []

def comp_guess(a,b):

    guess = (random.randint(a,b))

    if guess in bad_guesses:
        comp_guess(a,b)

    elif guess == my_num:
        print("You got it: ",my_num)
        print("Game Over")

    elif guess < my_num:
        a = guess
        bad_guesses.append(guess)
        print("Comp number: {}".format(guess))
        instruction = input("Smaller/Bigger: ")
        if instruction.lower() == "bigger":
            print(" ")
            comp_guess(a,b)
        else:
            print("Bad instruction!")

    elif guess > my_num:
        b = guess
        bad_guesses.append(guess)
        print("comp number: {}".format(guess))
        instruction = input("Smaller/Bigger: ")
        if instruction.lower() == "smaller":
            print(" ")
            comp_guess(a,b)
        else:
            print("Bad instruction!")



comp_guess(a,b)