Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 1.95 KB

exercise3.md

File metadata and controls

44 lines (31 loc) · 1.95 KB

implement a game of digits:

rule of the game.

  • one player thinks of a number of certain number of digits.
    • (eg for 4 digits number, he thought of 6789)
  • the other player start guessing the number.
    • (eg. other player guessed it is 2687)
  • the first player then has to say how many digits in the guesses number where right and how many were in the correct position.
    • (so in the guessed number in above example, 3 digits were correct: 6, 7, and 8 and only 8 was in correct position. so the first player says: 3 correct digits, 1 correct position)
  • second player retries the guess. and the process is repeated till the second player guesses the number correctly. the number of attempts is recorded and displayed as: 'You guessed the number in x guesses'

here let the program itself be the first player and guesses the random number of 3 digits(000 - 999). generate random string of from 0-9 using randint(0,9) method of previous exercise and concatenating the digit generated by the function.

from random import randint # we are using something from a package random
random_digit= randint(0, 9) # generates a random number from 0 to 9

# your code here

hint

# you can access some string's  nth item just like you are accessing an item from a list

random_string = "kamehameha"

print(random_string[0]) # prints k
print(random_string[1]) # prints a
print(random_string[2]) # prints m
print(random_string[-1]) # prints a

read the string manipulation article below for more knowledge.

BONUS: after your first iteration of successful implementation of the program, study the principle DRY(Dont Repeat Yourself) from the resource section below and try to refactor the repeated codes(if any) into a function.

resources: