0

I'm a tutor that is helping one of my students with a simple guessing game program. It's supposed to be similar to the board game Mastermind. This program should prompt the user to enter 5 distinct digits, and it should return the how many digits are correct and how many are in the right position. It does all of this, but it's not returning the correct values. I must have one of my loops messed up when the program is calculating these two numbers. If my spidey senses are correct, then I think the problem is in the numRight(x) function and the rightPosition(y) function. Any feedback would be lovely.

```
import random
def randomNumGenerator():
## this function constructs the five digit string that the
## user has to guess correctly.
concStr = '' ## accumulator for string concatenation
sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] ## list of ints
random.shuffle(sequence) ## shuffles the list
numToGuess = random.sample(sequence, 5) ## shortens the list to 5 elements
for i in range(5):
## this loop concatenates the list of ints into a single string
new = numToGuess.pop() ## gets the last element from the list
newStr = str(new) ## converts the element to a string
concStr += newStr ## concatenates
return concStr
def numRight(x):
rightCount = 0 ## counter for how many numbers are right
randNums = list(randomNumGenerator()) ## need list for loop
userNums = list(x) ## need list for loop
for userNum in userNums:
## this loop takes an element from the userNums list
## and checks to see if it is the same as any of the
## other elements in the randNums list. It will return
## how many numbers the user guessed correctly.
for randNum in randNums:
if(userNum == randNum):
rightCount = rightCount + 1 ## counter
return(rightCount)
def rightPosition(y):
posCount = 0
randPos = list(randomNumGenerator()) ## need list for loop
userPos = list(y) ## need list for loop
for u in userPos:
## this loop compares the positions of the elements in each list.
## if the lists have the same value at any position, then the
## counter is incremented.
## @return posCount numbers at correct positions
## @precondition elements in each list must be distinct
for r in randPos:
if(u == r):
if(userPos.index(u) == randPos.index(r)):
posCount = posCount + 1 ## counter
return(posCount)
def userGuess():
## this function simply gets the user's guess
guess = input("Enter your guess (5 distinct numbers): ") ## gets input from user
guessStr = str(guess) ## safety precaution, converts guess to a string
return guess
def mastermind(maxGuesses):
tryToGuessMe = randomNumGenerator()
for guess in range(maxGuesses):
aGuess = userGuess()
##print(aGuess)
aRightNum = numRight(aGuess)
##print(aRightNum)
aRightPos = rightPosition(aGuess)
##print(aRightPos)
print("You have", aRightNum, "numbers out of 5 correct.")
print("You have", aRightPos, "numbers in the right place.")
if aRightNum == 5:
if aRightPos == 5:
print("You win!")
break
print("Sorry, you lose.")
mastermind(20)
```