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

can someone please post the reverse version of number game.. as kenneth discussed

I am trying to code it reverse way as kennethe discussed when the user knows the number and computer guesses didnt see any thread on that i think it ll be a good discussion

1 Answer

Steven Parker
Steven Parker
229,921 Points

You didn't provide a link to the course or video, so I'm not sure what specific conditions should be observed. The computer could guess randomly or strategically, for example.

Here's a sample program which combines a random first guess with a strategic followup:

import random

low = 1
high = 100
answer = ''
tries = 0

print("Pick a number from {} to {} and I will try to guess it.".format(low, high))
input("Hit return when ready: ")
print("")

guess = random.randint(low,high)

while answer != "yes":
    answer = input("Is it {}? ".format(guess))
    if answer == "higher":
        low = guess + 1   
        guess = int((high - low) / 2 + low)
        tries += 1
    elif answer == "lower":
        high = guess - 1
        guess = int((high - low) / 2 + low)
        tries += 1
    elif answer == "yes":
        if (tries):
            print ("Yay!  I got it in {} tries!".format(tries + 1))
        else:
            print ("Holy Cow!  I read your mind!")
    else:
        print ('Please answer with "higher", "lower", or "yes".')