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

Luis Gutierrez
seal-mask
.a{fill-rule:evenodd;}techdegree
Luis Gutierrez
Python Development Techdegree Student 2,131 Points

Guess Game

hey guys. I'm basically a newbie, trying to write the guess a number and attempt count game. Any directions?? xD

import random

def start_game():

    print("                         =====================================")
print("                         =====================================")
print("                         #####################################")
print("########################## Welcome Player to the guessing_game.! ###########################")
print("=========================================================================================")
print("                           Enter a random number from 0 to 10")
print("=======================  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10========================")

secret_number = random.randrange(0,10)
guess_number = int(input("Enter an integer"))

while guess_number != secret_number:
        try:
            first_attempt = input("Invalid guess, try again: ")
            second_attempt = input("Enter a second time an integer")
            third_attempt = input("Enter a fourth time an integer")
            fourth_attempt = input("Enter a fourth wrong attempt")
            fifth_attempt = input("Enter a fifth attempt")
            sixth_attempt = input("Enter a sixth attempt")
            seventh_attempt = input("Enter a seventh input")
            eigth_attempt = input("Enter an eigth attempt")
            nineth_attempt = input("Enter a nineth attempt")
            tenth_attempt = input("Enter a tenth attempt. Invalid Guess.")
            eleventh_attempt = input("Enter an eleventh attempt.")
            twelveth_attempt = input("Twelveth wrong attempt")

            if guess_number > 10:
                print("Its lower")

            if guess_number < 0:
                print("It's higher")

        except ValueError:
            print("Oh no! That's not a valid value. Try again...")

print("The game is over")

start_game()

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Luis Gutierrez 👋

As a Techdegree student you have access to our exclusive Slack workspace dedicated to the Techdegree you're enrolled in. I'd highly recommend asking any project related questions on Slack as there you'll find dedicated staff and fellow students that are familiar with the projects and requirements you're working on.

That being said, looking at your code you're currently only checking a users guess once, when starting the while loop. if they guessed the number in one go the loop won't run, otherwise it will start prompting them all for those other attempts at once, without checking if any of them are matching the value stored in secret_number.

Instead of creating multiple variables prompting the user for an input, I'd recommend overwriting the guess_number inside of the loop. This way the loop will also have a way to break out (as soon as the conditional no longer evaluates to true). If you haven't done so already, I'd recommend checking out the practice on While Loops as this is using similar behavior 🙂

Hope this helps to get you going again! If not feel free to reach out on Slack and we'll be more than happy to elaborate 😄