Python guessing game hints and points systems -
i'm writing first python game , trying incorporate 2 elements i'm unsure how write it.
1: idea game generate random 3 digit number inbuilt random module (ie. 123) , user have 23 tries guess correct numbers. initiate asking user input 3 digits between 0-9. want create hint system user knows if on right track. see example in link below (i can't embed images apparently).
click see example input/output hints
- a "w" indicates of characters in guess wrong.
- one or more "x"s indicates have correct character, in incorrect position
- one or more "r"s indicates have correct character in right position
to kind of hint need create 3 separate numbers , combine them form target number or still able following code:
target = random.randint(111, 999)
i've started writing function takes in variables guess (this user has entered) , target (the generated number):
def get_hint(guess, target):
this far have gotten it. laughable, know. literally have no idea if possible create hint system.
2: have points system points start @ 10000 (if user guesses correctly first try) , decreases 10% each incorrect guess (second guess = 9000, third guess = 8100, etc.) 2 decimal places. have incrementing count amount of guesses user has tried when guess correct number following happens:
if guess == target: print("congratulations!") print("{} correct answer!".format(target)) print("you guessed correct answer in {} tries , scored {} points.".format(tries, points))
first point system trivial: have score varible , modify @ each round score=score*0.9
, round 2 decimals when printing "{:.2f}".format(score)
regarding hint system :
having list of 3 numbers far easier deal i'll assume target
, guess
have 1 of following format "123"
or [1,2,3]
(as strings can indexed lists)
the tricky part doing right comparisons because have take care of digit have been matched against target in order give "r"
in example case of guess=113 , target=333. here function job:
def hint(guess,target): if guess == target: print("win") else: #we store list keep track of numbers #already matched don't count them twice x , r r=[0]*len(target) #first check if there's direct match i,n in enumerate(guess): if guess[i] == target[i]: r[i]=1 #we make new lists without digits matched stripped_guess=[n i,n in enumerate(guess) if r[i] == 0] stripped_target=[n i,n in enumerate(target) if r[i] == 0] #we try count amount of x x=0 n in set(stripped_guess): #we count how many time given digit appears # in 2 lists, smallest our x amount x+=min(stripped_guess.count(n),stripped_target.count(n)) if sum(r) == 0 , x == 0: print("w") else: print("r"*sum(r)+"x"*x)
some tests:
>>> hint(guess="404",target="404") win >>> hint("135","331") rx >>>hint("11123","12133") rrrx
Comments
Post a Comment