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

Robert Richey
Courses Plus Student 16,352 PointsCode Review: Pick a Number! Any Number!
Hi everyone,
I really like the idea of posting code for review - it's a good way to get feedback, rather than just coding in a vacuum and being unsure if I'm doing something really wrong or if I'm on the right track.
Here is my number guessing game - I haven't watched the 'Solution' video yet! Please let me know what you like or don't like - feedback is important to me, thanks!
# A Game of Numbers (and Guessing)
#
# Easy mode:
# For the range end value, enter a value that equals a power of 2 (ex. 128)
#
# This will give the player enough guesses to always win by using
# a binary search pattern.
#
# Hard mode:
# For the range end value, enter a value that equals a (power of 2) -1 (ex. 127)
#
# With one remaining guess, using a binary search pattern, there
# will remain several valid numbers to choose from.
import math
import random
import os
#===============================================================================
#
# Index of Functions (in order of declaration)
#
# begin
# cls
# initRange
# promptUser
# main
#
#===============================================================================
#===============================================================================
#
# begin(data)
#
# data is a list containing [[start, end], rem, secret]
#
#===============================================================================
def begin(data):
secret = data[2]
while True:
guess = promptUser(data)
data[1] -= 1
guesses = data[1]
if guess == secret:
print("You WIN! The secret number is {}".format(secret))
exit()
elif guesses == 0:
print("You LOSE! The secret number is {}".format(secret))
exit()
elif secret < guess:
print("The secret number is less than {}.\n".format(guess))
else:
print("The secret number is greater than {}.\n".format(guess))
#===============================================================================
#
# cls()
#
# clear console screen
#
#===============================================================================
def cls():
os.system(['clear','cls'][os.name == 'nt'])
#===============================================================================
#
# initRange()
#
# return: list holding a numeric range
# (ex. [1, 100])
#
#===============================================================================
def initRange():
range = []
start = 1
msg = "Range of numbers will go from 1 through "
end = int(input(msg))
if end < 10:
end = 10
elif end <= start:
end = start * 10
cls()
range.append(start)
range.append(end)
return range
#===============================================================================
#
# promptUser(data)
#
# prompt user for their guess and validate it
#
#===============================================================================
def promptUser(data):
# I like passing a list of data around in functions like this, but below
# looks a bit ugly because it's not immediately obvious what data[0][0]
# is supposed to be; not sure what to do about that.
start = data[0][0]
end = data[0][1]
rem = data[1]
msg = "Type QUIT to quit.\n\n"
msg += "I am thinking of a number from {} to {}.\n\n".format(start, end)
msg += "You have {} guesses remaining.\n".format(rem)
valid = False
while not valid:
guess = input("{}> ".format(msg))
error = "\n{} is not a valid guess!".format(guess)
if guess == "QUIT":
exit()
cls()
try:
# if casting to an int fails, an exception will be thrown
guess = int(guess)
if guess < start or guess > end:
print(error)
else:
valid = True
except ValueError:
print(error)
# end while
cls()
return guess
#===============================================================================
#
# main()
#
#===============================================================================
def main():
cls()
data = []
range = initRange()
data.append(range)
# starting remaining guesses
rem = math.floor(math.log(range[1], 2))
data.append(rem)
secret = random.randint(1, range[1])
data.append(secret)
begin(data)
main()
2 Answers

Ryan Carson
23,287 PointsHi Robert, I've created a pull request. I've only been writing Python for a month, so don't take anything I say as gospel. Just tried to offer my advice :)
/cc Kenneth Love

Ryan Carson
23,287 PointsI'd love to give you feedback, but it's actually hard to do it this way. Can you upload your code to GitHub and then post the link to the repo here? That will allow me to do line-by-line comments, which will be more helpful to you. Also, I could even do a pull request and if you like the changes, you can merge them!
As an example, here's some code I just finished that I'm going to post to the Forum.

Robert Richey
Courses Plus Student 16,352 PointsHi Ryan,
I just added this program to my Github account - spent the past day going through the Git Basics course (which is excellent).
https://github.com/0x0936/treehouse-python/tree/master
Thanks!

Ryan Carson
23,287 PointsThanks Robert! I'll review your code as soon as I can :)
Robert Richey
Courses Plus Student 16,352 PointsRobert Richey
Courses Plus Student 16,352 PointsA dictionary was exactly the data-structure needed to make the code more readable. Really appreciate the feedback!
Ryan Carson
23,287 PointsRyan Carson
23,287 PointsHappy I could help! :)