Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Hanifah W
Hanifah W
395 Points

Numbersgame.py an extension to the code gone awry. Can't get it to catch duplicates.

I'm not sure if you are still up, but ... No more errors . this script is not however doing two things.

  1. Once the user guesses the number it should account for how many attempts a user made to guess the number - how many times he/she duplicated a number. << I am confused with format and operands here.

  2. It should give the message "Ypu already guessed that number, try again when they input a duplicate number.

It late here but I really need to figure this out within the next hour. I'm determined. I hope you can help.

import random

def game():
# generate a random number between 1 -100 and initiate repeat guest list
    secret_num=random.randint(1,10)
    already_guesses = []
    number_guesses = 0
    final_count = number_guesses - len(already_guesses)

    while True:
# catch someone if the submit a non intergers  
        try:
            guess = int(input('Guess a number> '))
        except ValueError:
            print('{} That isn\'t a number'.format(guess))
        number_guesses += 1
        already_guesses.append(guess)
# help with guesses and announce if it is correct number! 
# catch someone if they submit the same number  
        if guess == secret_num:
            print("whooohoo! you guessed it, my number is {}".format(secret_num))

            print("It took you {} guesses".format(number_guesses))
            break
        elif guess < secret_num:
            print("You\'re getting hot, try a higher number")
        elif guess > secret_num:
            print("You\'re cold, try a lower number")   
        else:
            guess in already_guesses
            print("You already guessed that number, try again")

    else:
        play_again = input("Do you wanna play again? y/n")
        if play_again != 'n':
            game()
        else:
            print("Bah Bye!")

game()        

1 Answer

Sorry you didn't get an answer straight away, but if you want to catch the duplicates, you need to check for it before you check if it is higher or lower, since it will always match at least one of the other conditions and therefore will never make it to the last one.

Also, you seem to have an extra else block at the end... You shouldn't need that, but instead just place the code outside the while loop:

    else:  # don't need this one, un-indent everything below by four spaces
        play_again = input("Do you wanna play again? y/n")
        if play_again != 'n':
            game()
        else:
            print("Bah Bye!")

Like so:

    play_again = input("Do you wanna play again? y/n")
    if play_again != 'n':
        game()
    else:
        print("Bah Bye!")