Home > Software engineering >  How do you show a user that their 3 digit integer input has the same numbers as a computer generated
How do you show a user that their 3 digit integer input has the same numbers as a computer generated

Time:04-20

I am creating a guessing game where you need to guess the exact number that the computer (i.e Python) has generated. I am making it have an easy mode (which generates a 3-digit number for the user to guess) and a hard mode (which generates a 4-digit number for the user to guess).

However, for the easy mode, I want to be able to tell the user that they have one (or more) number in their guess which is correct but it is in the wrong position.

So far, I have used index positions for the computer to note what numbers (in what order) are in the computer-generated, as shown in my code below:

def easy_mode(): 
    easy_num = randint(100,999)
    easy_digit_splitter1 = str(easy_num)[0]
    easy_digit_splitter2 = str(easy_num)[1]
    easy_digit_splitter3 = str(easy_num)[2]

I know that I will need a variable that stores the user's guess under the code above (as well as a while loop), but how do I compare both the "easy_num" and the variable which will store the user's guess and tell the user that they have a correct number in their guess but it's in the wrong position?

CodePudding user response:

I'd try splitting the user guess the same way you split the computer value, and then saving them all to a 2x3 2D list:

digitList = [[easy_digit_splitter1, easy_digit_splitter2, easy_digit_splitter3], [guess_digit1, guess_digit2, guess_digit3]]

Then, run a nested for loop that checks the first array values against the second list values:

for i in range(0, digitList[0].len()):
    for j in range(0, digitList[1].len()):
        if (digitList[0][i] = digitList[1][j]) and (i != j):
            print("Right value, wrong place")

That should print "Right value, wrong place" every time the user gets a correct digit but in the wrong spot. It won't tell them what digit that is, but these two bits of code should suffice as the brains of this checker.

Please let me know if you have any problems with this and if you need any clarifications!

CodePudding user response:

Once you get your user guess and your random number you can convert to list and iterate:

easy_num_list = list(str(easy_num))
user_guess_list = list(user_guess)
for i in range(0, len(user_guess_list)):

Then in that loop you can do your testing:

easy_num_list = list(str(easy_num))
user_guess_list = list(user_guess)
for i in range(0, len(user_guess_list)):
    #use the `in` operator to see if this digit from the user's guess is in the randomly generated number
    if user_guess_list[i] in easy_num_list:
        print("number",user_guess_list[i],"is in the random number")
        #check to see if this digit from the user's guess is in the exact same index/position as the randomly generated number
        if user_guess_list[i] == easy_num_list[i]:
            print("and it's in the right spot")
        else:
            print("but it's not in the right spot")
    else:
        print("number",user_guess_list[i],"is not in the random number")

There's more clean and elegant ways to do this logic, but this should get you in the ballpark.

  • Related