228k views
4 votes
Write a program for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 53840, and the user guesses 83241, the digits 3 and 4.

User Pvorb
by
8.4k points

1 Answer

3 votes

Answer:

from random import randint

winner = False

number = str(randint(10000, 99999))

while (not winner):

correct = 0

total = 0

guess = input()

if(number==guess):

winner = True

else:

for i in range(5):

if(number[i] == guess[i]):

correct+=1

total+=int(number[i])

print('Correct: '+ str(correct))

print('Total: '+ str(total))

print('Winner')

Step-by-step explanation:

I´m gonna show a solution in python 3

Step 1 import library for generate random number

from random import randint

Step 2 create necesary variables to get the number and if the player is winner or not

winner = False

number = str(randint(10000, 99999))

Step 3 loop while the player is not the winner

while (not winner):

Step 4 get the player guess number

guess = input()

Step 5 validate if the player win

if(number==guess):

winner = True

Step 6 if the player is not winner review what numbers are in the correct position

for i in range(5):

if(number[i] == guess[i]):

correct+=1

total+=int(number[i])

Step 7 print the hint

print('Correct: '+ str(correct))

print('Total: '+ str(total))

User Kamil Solecki
by
9.6k points