Answer to Question #346200 in Python for madhu

Question #346200

Guessing numbers

Rahul is playing a guessing number game with computer . the objective of the game is to guess the number that computer think . A guess is "correct" when the guess exactly matches the computer number. After each guess by Rahul, the computer will give a score comprising black & white coins:

Black coins = guessed digit is present in the computer's number and in same position

white coins = guessed digit is present in the computer's number and in another position

your task is to find out the number of black coins and number of white coins

Input:

The first line of input contains two space-separated integers

denoting the Rahul's Guess and the computer's Number.

Output:

The output should be a string denoting the number of Black coins and the number of White coins in the format as shown in the same output.

sample output:

given rahul=1423, computer=1234

so the output should be "blacks : 1 whites : 3 "

sample input2;

1423 5678

blacks  :  0 whites : 0


1
Expert's answer
2022-05-30T08:17:28-0400
while True:
    try:
        inp_num=input('Enter Rahul\'s Guess and the computer\'s Number: ')
        numbers=inp_num.split()
        if len(numbers)!=2:
            raise Exception
        else:
            if len(numbers[0])!=len(numbers[1]):
                raise Exception
            num_Rahul=int(numbers[0])
            num_computer=int(numbers[1])
            break

    except ValueError:
        print ('You can only enter integer.')

    except Exception:
        print ('You can only enter two integers of the same length.')
whites=0
blacks=0
for i in range(0, len(numbers[0])):
    if int(numbers[0][i])==int(numbers[1][i]):
        blacks+=1
    elif numbers[1].find(numbers[0][i])!=-1:
        whites+=1
        
print('blacks: {} whites: {}'.format(blacks, whites))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS