hi, I was writing a code on bulls and cows game and although it is working for most part, the last while loop has issues, when the correct guess is made the loop raises an error instead of terminating, anyone who can help me point out what the issue is?
got it right, thw double while loops where repetitive and unnecessary, had no break point
'''this game matches the user input with a random 4 digits number, The game works like this: Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. ''' def user(): user_input=(input('enter a 4 digit number: ')) return user_input import random random_number = str(random.randint(1000,9999)) def logic(user_input): if len(user_input) != 4: print('please enter a 4 digits number: ') user_input=user() else: pass while random_number != user_input: count=0 cows=0 bulls=0 for i in user_input: if random_number[count]==user_input[count]: cows+=1 count+=1 elif i in random_number: bulls+=1 count+=1 else: count+=1 print(random_number) return cows,bulls if __name__=='__main__': cows, bulls = logic(user()) while cows!=4: print(cows, ' cows and ', bulls, ' bulls') cows, bulls = logic(user()) print(cows, ' cows and ', bulls, ' bulls') print(' you guessed right, the number is ', random_number)
got it right, thw double while loops where repetitive and unnecessary, had no break point
'''this game matches the user input with a random 4 digits number, The game works like this: Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. ''' def user(): user_input=(input('enter a 4 digit number: ')) return user_input import random random_number = str(random.randint(1000,9999)) def logic(user_input): if len(user_input) != 4: print('please enter a 4 digits number: ') user_input=user() else: pass if user_input == random_number: cows=4 bulls=0 else: count=0 cows=0 bulls=0 for i in user_input: if random_number[count]==user_input[count]: cows+=1 count+=1 elif i in random_number: bulls+=1 count+=1 else: count+=1 print(random_number) return cows,bulls if __name__=='__main__': cows, bulls = logic(user()) while cows!=4: print(cows, ' cows and ', bulls, ' bulls') cows, bulls = logic(user()) print(' you guessed right, the number is ', random_number)