How to use In Operator for Integers?

Viewed 34

everyone. Today I saw a code to create a 4-digit bulls and cows game. But I am curious if anyone can explain to me how did user_guess in numbers work at line 12? From what I understand, number is a string and user_guess is an integer which shouldn't work right? The code is below.

import random
secret_number = str(random.randint(1000, 9999))
print("Guess the number. It contains 4 digits.")

remaining_try = 7

def get_bulls_cows(number, user_guess):
 bulls_cows = [0,0] #cows, then bulls
 for i in range(len(number)):
    if user_guess[i] == number[i]:
        bulls_cows[0] += 1
    elif user_guess[i] in number:
        bulls_cows[1] += 1
return bulls_cows

while remaining_try > 0:
player_guess = input("Enter your guess: ")

if player_guess == secret_number:
    print("Yay, you guessed it!")
    print("YOU WIN.")
    break
else:
    bulls_cows = get_bulls_cows(secret_number,player_guess)
  
    print("Bulls: ",bulls_cows[0])
    print("Cows: ",bulls_cows[1])

    remaining_try -= 1

    if remaining_try < 1:
        print("You lost the game.")
        break
0 Answers
Related