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
Eugene Paitoo
3,922 PointsNumber game
Hello, I've got the idea of building the number game where the computer let's the user guesses a number as to whether is right or wrong BUT building a reverse of the game (where the user let's the computer to guess a number) has become difficult for me. I need a little help with it.
Thanks!!
Agustin Fitipaldi
1,644 PointsI tried to do that, but I think I messed up in regards to the structuring of it, because I ended up with too much code, and an extremely confusing one at that:
import random
import time
import os
def spause():
time.sleep(1)
def pause():
time.sleep(2)
def clear():
os.system('cls')
c_guesses = []
def main():
print 'Please guess a number between 1-50'
honesty = 1
while honesty:
try:
user_number = int(raw_input('> '))
except ValueError:
print 'A number please'
spause()
clear()
else:
if user_number <= 50 and user_number >= 1:
print 'Thank you'
honesty = 0
else:
print 'Between 1-50'
print 'I will now try and guess your number'
cg1 = random.randint(1, 50)
c_guesses.append(cg1)
print 'Is {} your number?'.format(cg1)
correct = raw_input('> ').lower()
if correct == 'h':
different = 1
while different:
cg2 = random.randint(cg1, 50)
if cg2 > cg1:
print 'Is {} your number?'.format(cg2)
c_guesses.append(cg2)
correct = raw_input('> ').lower()
different = 0
else:
return
elif correct == 'l':
different = 1
while different:
cg2 = random.randint(1, cg1)
if cg2 < cg1:
print 'Is {} your number?'.format(cg2)
c_guesses.append(cg2)
correct = raw_input('> ').lower()
different = 0
else:
return
if correct == 'h':
different = 1
while different:
cg3 = random.randint(cg2, 50)
if cg3 > cg1 and cg3 > cg2:
print 'Is {} your number?'.format(cg3)
c_guesses.append(cg3)
correct = raw_input('> ').lower()
different = 0
else:
return
elif correct == 'l':
different = 1
while different:
cg3 = random.randint(1, cg2)
if cg3 < cg1 and cg3 < cg2:
print 'Is {} your number?'.format(cg3)
c_guesses.append(cg3)
correct = raw_input('> ').lower()
different = 0
else:
return
if correct == 'h':
different = 1
while different:
cg4 = random.randint(cg3, 50)
if cg4 > cg3 and cg4 > cg2 and cg4 > cg1:
print 'Is {} your number?'.format(cg4)
c_guesses.append(cg4)
correct = raw_input('> ').lower()
different = 0
else:
return
elif correct == 'l':
different = 1
while different:
cg4 = random.randint(1, cg3)
if cg4 < cg3 and cg4 < cg2 and cg4 < cg1:
print 'Is {} your number?'.format(cg4)
c_guesses.append(cg4)
correct = raw_input('> ').lower()
different = 0
else:
return
if correct == 'h':
different = 1
while different:
cg5 = random.randint(cg4, 50)
if cg5 > cg4 and cg5 > cg3 and cg5 > cg2 and cg5 > cg1:
print 'Is {} your number?'.format(cg5)
c_guesses.append(cg5)
correct = raw_input('> ').lower()
different = 0
else:
return
elif correct == 'l':
different = 1
while different:
cg5 = random.randint(1, cg4)
if cg5 < cg4 and cg5 < cg3 and cg5 < cg2 and cg5 < cg1:
print 'Is {} your number?'.format(cg5)
c_guesses.append(cg5)
correct = raw_input('> ').lower()
different = 0
else:
return
if correct == 'h':
different = 1
while different:
cg6 = random.randint(cg5, 50)
if cg6 > cg5 and cg6 > cg4 and cg6 > cg3 and cg6 > cg2 and cg6 > cg1:
print 'Is {} your number?'.format(cg6)
c_guesses.append(cg6)
correct = raw_input('> ').lower()
different = 0
else:
return
elif correct == 'l':
different = 1
while different:
cg6 = random.randint(1, cg5)
if cg6 < cg5 and cg6 < cg4 and cg6 < cg3 and cg6 < cg2 and cg6 < cg1:
print 'Is {} your number?'.format(cg6)
c_guesses.append(cg6)
correct = raw_input('> ').lower()
different = 0
else:
return
if correct == 'h':
different = 1
while different:
cg7 = random.randint(cg6, 50)
if cg7 > cg6 and cg7 > cg5 and cg7 > cg4 and cg7 > cg3 and cg7 > cg2 and cg7 > cg1:
print 'Is {} your number?'.format(cg7)
c_guesses.append(cg7)
correct = raw_input('> ').lower()
else:
return
elif correct == 'l':
different = 1
while different:
cg7 = random.randint(1, cg6)
if cg7 < cg6 and cg7 < cg5 and cg7 < cg4 and cg7 < cg3 and cg7 < cg2 and cg7 < cg1:
print 'Is {} your number?'.format(cg7)
c_guesses.append(cg7)
correct = raw_input('> ').lower()
different = 0
else:
return
main()
It has too many errors to count probably, but hey, if you want to try it out, just know that you should avoid this method. lol
2 Answers
Steven Parker
243,200 PointsYou're on the right track, but you don't need to retain every guess, just the most recent high and low one. Then you can use them to constrain the next random choice. That should allow you to reduce the code considerably.
Agustin Fitipaldi
1,644 Pointsbut then how do I check if the recent guess was no equal or smaller or bigger than the rest? thats what I ended up getting confused on
Steven Parker
243,200 PointsIf I understand how you want this program to work, you rely on the user to tell you if the guess was too low, too high, or correct. So based on the user's answer, you might store the current guess in either "high" or "low", and then generate the next guess (which would be done in only one place in the main loop) as "cg = random.randint(low+1, high-1)"
Agustin Fitipaldi
1,644 Pointsaha! you gave me an idea, ill try it out and get back to you if it works. thanks!
Steven Parker
243,200 PointsSteven Parker
243,200 PointsI believe that's actually a suggested extra exercise in one of the courses. Could you be more specific about what kind of help you need with it?