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 Introduction

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

My variation on the python number game

Hi all,

Here's my variation of the Python Basics number game. What I want to do is let the user know how many attempts it took to get to the right answer.

number_game2.py
# import random module from the standard library
import random 

#generate a random number between 1 and 10
secret_num = random.randint(1,10)
attempts = []

while True:
    #get an number guess from the player
    guess = int(input("Guess a number between 1 and 10: "))

    #compare guess to secret number
    if (guess==secret_num):
        print("You got it! My number was {}".format(secret_num))
        break;
    else: 
        print("Sorry, that's not the one")
        for attempt in attempts:
            attempt = attempt + 1
        #print hit/miss

print("You did it in {} tries.".format(attempts))   

First, I tried simply adding to the attempts variable with a loop but the interpreter kept telling me int() which I took to be the data type of attempts was not an interable.

But I know an array is an iterable so I tried an empty one.

As an example, here's what I get.

Guess a number between 1 and 10: 4 Sorry, that's not the one Guess a number between 1 and 10: 6 Sorry, that's not the one Guess a number between 1 and 10: 2 You got it! My number was 2 You did it in [] tries.

I feel like I'm getting close. any ideas? :)

1 Answer

Stuart Wright
Stuart Wright
41,120 Points

How about this? Have commented out lines of yours which I removed, and put comments on same lines as my own additions:

# import random module from the standard library
import random 

#generate a random number between 1 and 10
secret_num = random.randint(1,10)
#attempts = []
attempts = 1  # A simple counter rather than a list makes more sense to me here

while True:
    #get an number guess from the player
    guess = int(input("Guess a number between 1 and 10: "))

    #compare guess to secret number
    if (guess==secret_num):
        print("You got it! My number was {}".format(secret_num))
        break;
    else: 
        print("Sorry, that's not the one")
        # for attempt in attempts:
            # attempt = attempt + 1
        attempts += 1  # increment integer counter rather than the list logic you had above
        #print hit/miss

print("You did it in {} tries.".format(attempts))
Steven Parker
Steven Parker
231,236 Points

I was about to suggest the same, except you may want to set an initial value of 1 instead of 0.

Stuart Wright
Stuart Wright
41,120 Points

Oops, yes of course, thanks. Will edit my post.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I think I was trying to be too clever by adding a loop to the code. :)

I did start by trying attempts as a simple integer... but yea... the if statement was all I really needed. Thanks. ;)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I even had the time to add and test more functionality to the program. Since I'm proud of my work, here's what else I've done. A different message if the number was correctly guessed first attempt.

# import random module frm the standard library
import random 

#generate a random number between 1 and 10
secret_num = random.randint(1,10)
attempts = 0

while True:
    #get an number guess from the player
    guess = int(input("Guess a number between 1 and 10: "))

    #compare guess to secret number
    if (guess==secret_num):     
        attempts += 1
        print("You got it! My number was {}".format(secret_num))
        break;
    else: 
        print("Sorry, that's not the one")
        attempts += 1
        #print hit/miss

if attempts == 1:
    print("Well done!!! You did it in {} try!!".format(attempts))   
else:
    print("You did it in {} tries.".format(attempts))
Steven Parker
Steven Parker
231,236 Points

You don't even need to use format when you know what the value is:

if attempts == 1:
    print("Well done!!! You did it in only one try!!")   
else:
    print("You did it in {} tries.".format(attempts))