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
Chase Frankenfeld
6,137 PointsNumber game extra credit
Here is my extra credit.
My main issue, is that I can't figure out how to accommodate for when the input provided is not 'high', 'low' or 'correct', and so the computer repeats its guess, or asks for another input, without the computer selecting another number. I figure the try loop is the best, but wasn't sure how to apply it. Appreciate the help as always.
# ********** GOALS **********
# Make the computer guesses the number
import random
import os
import sys
# Inputs
highest_num = 10
lowest_num = 1
count = 5
# Clear
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
# Instructions
def instructions():
clear()
print("Welcome friend :)")
print("")
print("Here are the rules:")
print("")
print("1. You must pick a number between {} and {}.".format(lowest_num, highest_num))
print("""2. You must remember this number. Don't change it and be a cheat.
3. The computer will guess a number.
4. If the computer's guess is too high. Type "high"!
5. If the computer's guess is too low. Type "low"!
6. If the computer's guess is correct. Type "correct"!
7. If the computer takes more than {} guesses. You will WIN.
""".format(count))
start = input("Please press enter/return to continue to play again, or Q to exit ")
if start.lower() == 'q':
print("Bye! Have a good day :)")
sys.exit()
else:
game()
def exit():
print ('Hope you have enjoyed the guessing game.')
exit = input("Please press enter/return to continue to play again, or Q to exit ")
if exit.lower() == 'q':
clear()
print("Bye! Have a good day :)")
sys.exit()
else:
game()
# THE GAME
def game():
high_num = highest_num
low_num = lowest_num
clear()
total_guesses = []
# Keep it to 5 guesses
while len(total_guesses) < count:
#limit the amount of guesses
guess = random.randint(low_num, high_num)
print("My number is {}".format(guess))
answer = input("Is the number too high, too low, or CORRECT? ")
if guess not in total_guesses:
# Feedback to computer
if answer.lower() == "high":
clear()
print("I take it {} is too high. I'll try a number a little lower!".format(guess))
high_num = guess - 1
# I tell the computer if the number is too low - "low"
elif answer.lower() == "low":
clear()
print("I take it {} is too low. I'll try a number a little higher!".format(guess))
low_num = guess + 1
# I tell the computer if the number is too high - "high"
elif answer.lower() == "correct":
clear()
print("I'll have to make it harder next time. That was way too easy!!!")
exit()
else:
clear()
print("You have not typed an appropriate entry. Please type either 'high', 'low', or 'correct'.")
total_guesses.append(guess)
print("I have guessed {}. Nowww......".format(total_guesses))
print('You WIN!!')
exit()
while True:
instructions()
game()
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsOne approach is to wrap the input in a loop
# replace this
answer = input("Is the number too high, too low, or CORRECT? ")
# with this
while True:
answer = input("Is the number too high, too low, or CORRECT? ").lower()
if answer in ['high', 'low', 'correct']:
break
else:
clear()
print("You have not typed an appropriate entry. Please type either 'high', 'low', or 'correct'.")
Chase Frankenfeld
6,137 PointsChase Frankenfeld
6,137 PointsThanks as always Chris. Again, the answer is so simple. Slowly getting the hang of this.