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 trialHanifah W
395 PointsNumbersgame.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.
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.
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
Iain Simmons
Treehouse Moderator 32,305 PointsSorry 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!")