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 trialwesley jackson
2,436 PointsNumber Game Refinement
After this section of code was added the game does not ask for input it just does not run.
play_again = input("Do you want to play again? Y/n") if play_again.lower() != 'n': game() else: print("bye!") even after removing .format(guess) form the try block
:-( Much appreciation for any pointers.
Thank you. Aplogies but this seems the only way I can get the code to be readable as I have tried to use markup but without success
import random
def game():
# generate a random number between 1 and 10
secret_number = random.randint(1,10)
guesses = []
while len(guesses) < 5:
try:
# get a number guess from the player
guess = int(input("Guess a number between 1 and 10 :"))
except ValueError:
print(" That isn't a number")
else:
# compare to secret number
if guess == secret_number:
print("You got it! My number was {}".format(secret_number))
break
elif guess < secret_number:
print("Number is higher than {}".format(guess))
else:
print("Number is lower than {}".format(guess))
guesses.append(guess)
else:
print("You did not get it my number was {}".format(secret_number))
play_again = input("Do you want to play again? Y/n")
if play_again.lower() != 'n':
game()
else:
print("bye!")
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsWhat calls game()
to start play? The code as written defines a game()
function but does not call it.
add call to game at end of file:
game()
add Python standard hook at end of file to execute if module executed:
if __name__ == "__main__":
game()
wesley jackson
2,436 Pointswesley jackson
2,436 PointsHi Chris, Thank you but alas it is still not working and after adding the changes and snippet of code the last 'else' statement is said to be out of indentation. I will try and have another go wth it.
Thank you
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsKeep at it! Repost your latest code.