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 trialArthur Walton
Python Web Development Techdegree Student 3,906 Pointsnumber guessing game help / python
Hey There, So i'm having a bit of trouble with my code. I feel as if it's right, but for some reason it won't run. Is there just some spacing issues, or is there something deeper wrong with my code? Thanks in advance for helping me out.
import random
min_number = 1
max_number = 20
random_number = random.randint(min_number, max_number)
guesses = []
def start_game():
print("""
Welcome to Sonny's Spectacular Number Guessing Game!!!
How to play: Guess a number between 1 and 20. Don't worry if you get the number incorrect, Sonny will be there to help you along the way. Good Luck!
""")
while guesses != 0:
while True:
try:
guess = int(input("Choose a number between 1 and 20"))
if guess < max_number or guess > min_number:
except ValueError:
print("PLease choose a number inside the range of 1 through 20")
continue
guesses +=1
if guess == random_number:
print("Sonny thinks you've found the right number!! It took you {} guesses to get the right number".format(guesses))
break
elif guess < random_number:
print("Sonny thinks your number is a bit cold, try something higher!")
continue
elif guess > random_number:
print("Sonny thinks your number is a bit hot, try something lower!")
continue
return guesses
while True:
print("Sonny wants to know if you want to play again? Y/N")
restart = input("")
if restart.lower() == "y":
continue
elif restart.lower() =="n"
print("Thanks again for playing")
break
start_game()
1 Answer
Steven Parker
229,744 PointsPython is very difficult to analyze without the original indentaion. To preserve the appearance of your code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
In the meantime, one error the I spotted already is that the "except" should not be inside a conditional ("if") that separates it from the "try".