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

Python Python Basics (2015) Number Game App Number Game Refinement

Ali Dahud
Ali Dahud
3,459 Points

is it alright the way I have done it?

import random

secret_num=random.randint(1,10)

answer=""

def play_game():

number_of_guesses = 1

while number_of_guesses<5:

    try:

        guess=int(input("Enter a guess between 1-10: "))

    except ValueError:

        print("A Number pllease!")

    else:

        if guess>secret_num:

            print("you entered too high!")

        if guess<secret_num:

            print("you entered too low!")

        if guess==secret_num:

            print("you guessed it right the number was: {}".format(secret_num))

            break

    number_of_guesses+=1

answer = input("Enter Done if you're done playing: ")

if answer=="Done":

    print("Thank you for playing!")

else:

    play_game()

play_game()

AND WHAT'S the purpose of creating a game that loops until forever if we want it if we guessed the number already?