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

Sacheath Son
seal-mask
.a{fill-rule:evenodd;}techdegree
Sacheath Son
Data Analysis Techdegree Student 3,357 Points

Python Project 1 (trying to figure out how to restart program)

I got everything else to work but I just can't seem to think of how to restart the program. I tried looking through the forums and other submissions on GitHub but everyone either did different levels of features to the program. Since it's part of the definition for the start_game function, is it possible or do I need to remove it from the definition? I'm just not sure because the instruction told me to keep everything in the definition of start game. It's towards the bottom in a while block (line 59).

Thank you!

import random
from statistics import mode, median, mean

def start_game():
    print("Welcome to Team Treehouse's first project: Number Guessing Game!")

    answer = random.randrange(1,100)
    actual_guesses = []
    high_score = []  



    while True:
        try:
            confirm_game = input("Are you sure you want to give it a shot? Y/N: > ")
            if confirm_game.lower() == 'n':
                print("Sorry to hear that, have a nice day!")
                exit()
            if confirm_game.lower() == 'y':
                if len(high_score) < 1:
                    print("There is no current high score!")
                else:
                    print("The current high score is {} attempts!".format(min(high_score)))
                print("Pick a number between 1 and 100!")
                break
            else:
                raise ValueError()
        except ValueError:
            print("That's not a valid selection. Please enter either Y or N.")
            continue

    while True:
        try:
            player_guess = int(input("> "))
            if player_guess <= 0 or player_guess > 100:
                raise ValueError()
        except ValueError:
            print("That's not a valid guess. Please only use whole numbers between 1 and 100.")
            continue

        if player_guess > answer:
            print("It's lower!")
            actual_guesses.append(player_guess)
            continue
        elif player_guess < answer:
            print("It's higher!")
            actual_guesses.append(player_guess)
            continue
        else:
            print("You got it right in only {} attempts!".format(len(actual_guesses)))
            high_score.append(len(actual_guesses))
            break

    while True:
        try:
            restart = input ("How about another round? Y/N: > ")
            if restart.lower() == 'y':                
                print("Coming right up!")
                #how do I restart the game?
            if restart.lower() == 'n':
                print("""
        Sorry to hear that. Here's your stats for this round!

        Attempts: {}
        Mean Guess: {}
        Median Guess: {}
        Mode Guess: {}

        Come back later if you ever want to try again!
                """.format(total_guesses, round(mean(actual_guesses)), round(median(actual_guesses)), round(mode(actual_guesses))))
                exit()
            else:
                raise ValueError()
        except ValueError:
            print("Sorry that's not a valid selection. Please enter either Y or N.")

start_game()

1 Answer

Steven Parker
Steven Parker
230,274 Points

One quick change that would do it is to call start_game() again at the point you want the game to restart. Since you're already in that function, a more elegant way might be to restructure the way the loops work but that would require quite a bit more effort.

For future questions, it's helpful if you associate the question with the course.
Take a look at this video about Posting a Question.