Python Forum
while not terminating - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: while not terminating (/thread-15320.html)



while not terminating - gitiya - Jan-13-2019

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?


'''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)



RE: while not terminating - ichabod801 - Jan-13-2019

BTW, it's real helpful if you give us the (full) error text. I could have figured out straight from that rather than fiddling with your game.

If they guess right, random_number == user_input, so the while loop in logic is never run. That means logic returns None, because it never encounters the return statement on line 32.