count - Python guessing game, counting number of guesses -


my intro computer science teacher gave dice guessing game challenge, , while i've managed figure out of components, final piece return correct number (once they've guessed it) , number of tries took them figure out. code returns of that, doesn't account if person guessing guesses same number twice. there way tell them how many guesses took them find number while disregarding repeated numbers?

this code have far:

import random  give_number = input("we roll 6 sided dice. think number be?\n ") guess_number = 1  dice = random.randint(1,6) while give_number != dice:     if give_number > dice:         give_number = input("sorry, answer high! try again!\n ")         guess_number = guess_number +1     if give_number < dice:         give_number = input("sorry, answer low! try again!\n ")         guess_number = guess_number +1  print "congratulations, right, answer {}! took {} tries.".format(dice, guess_number) 

to detect repeated guesses, have keep track of previous guesses.

we need know if particular number has been guessed before - not how many times has come up.

this perfect place using set (see python docs).

begin empty set (rather beginning guess_number = 1), add each guess set.

once person has guessed correctly, can check size of set - number of guesses, ignoring duplicates.


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -