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 trialjohn larson
16,594 PointsNumber game extra credit. I think I got it. It works anyway.
import random
def game():
def pc_guess(a, b):
return random.randint(a, b)
a = 1 #default value for a
b = 10 #default value for b
my_num = input("Enter a number 1 - 10: ")
try:
my_num = int(my_num)
except ValueError:
print("ERROR: enter a number 1 - 10, ie: 1,2,3...")
if ValueError:
game() #restart game if error is called
else: #if no error, run the following code
times = 0 #just to track how many loops run
while True:
times += 1 #each iteration add 1
rn = pc_guess(a, b)#call the random number fn, default values
print("try {}, pc guess is {}".format(times, rn)) #show iteration and pc guess
print("a = {} and b = {}".format(a, b)) # show the current value of a and b
if rn < my_num:
a += 1 # raise lower param if the pc guess is low
elif rn > my_num:
b -= 1 #lower upper param if pc guess is high
else: # if it's not < or >
print("{}, Everyones a winner!".format(rn))
break
replay = input("Play again? Y/n ")
if replay != "n":
game()
else:
print("Thank you for playing.")
game()