rookie_error311 - 1 year ago 108

Python Question

I'm writing my first python game and trying to incorporate two extra elements but I'm unsure how to write it.

1: The idea is that the game will generate a random three digit number with the inbuilt random module (ie. 123) and the user will have 23 tries to guess the correct numbers. It will initiate by asking the user to input three digits between 0-9. I want to create a hint system so that the user knows if they are on the right track. See the example in the link below (I can't embed images apparently).

Click to see example Input/Output for hints

- A "W" indicates that all of the characters in the guess are wrong.
- One or more "X"s indicates that they have a correct character, but in an incorrect position
- One or more "R"s indicates they have a correct character in the right position

To get this kind of hint will I need to create 3 separate numbers and combine them together to form the target number or will I still be able to do it with the following code:

`target = random.randint(111, 999)`

I've started writing a function that takes in the variables guess (this is what the user has entered) and target (the generated number):

`def get_hint(guess, target):`

This is as far as I have gotten with it. Laughable, I know. I literally have no idea if it even possible to create this hint system.

2: I would also like it to have a points system where the points start at 10000 (if the user guesses correctly first try) and decreases by 10% each incorrect guess (second guess = 9000, third guess = 8100, etc.) to two decimal places. I have it incrementing a count for the amount of guesses the user has tried so when they guess the correct number the following happens:

`if guess == target:`

print("Congratulations!")

print("{} was the correct answer!".format(target))

print("You guessed the correct answer in {} tries and scored {} points.".format(tries, points))

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

First the point system is fairly trivial: just have a score varible and modify it at each round `score=score*0.9`

and round it to 2 decimals when printing with `"{:.2f}".format(score)`

**Regarding the hint system :**

Having a list of three numbers will be far easier to deal with so I'll assume `target`

and `guess`

have one of the following format `"123"`

or `[1,2,3]`

(as strings can be indexed as lists)

The tricky part is doing the right comparisons because you have take care of what digit have already been matched against the target in order to give only `"r"`

in the example case of guess=113 and target=333. Here is a function that does the job:

```
def hint(guess,target):
if guess == target:
print("win")
else:
#we store a list to keep track of the numbers
#already matched so we don't count them twice as x and r
r=[0]*len(target)
#first we check if there's a direct match
for i,n in enumerate(guess):
if guess[i] == target[i]:
r[i]=1
#we make new lists without the digits that matched
stripped_guess=[n for i,n in enumerate(guess) if r[i] == 0]
stripped_target=[n for i,n in enumerate(target) if r[i] == 0]
#we will now try to count the amount of x
x=0
for n in set(stripped_guess):
#we count how many time that given digit appears
# in the two lists, the smallest is our x amount
x+=min(stripped_guess.count(n),stripped_target.count(n))
if sum(r) == 0 and 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
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**