JOHNYTHEWINNER

Bulls and cows

Jun 23rd, 2020
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def game(num_digits):
  5.     listnum = [random.randint(0, 9) for n in range(num_digits)]
  6.     guesses_counter = 0
  7.     print('To give up, please type "Give up".')
  8.     while True:
  9.         print("Please guess " + str(num_digits) + " digit number:")
  10.         guess = input()
  11.         if guess == "Give up":
  12.             print(f"You gave up, the number is {listnum}.")
  13.             break
  14.         else:
  15.             guess = [int(i) for i in guess]
  16.         guesses_counter += 1
  17.         if guess == listnum:
  18.             print("You won")
  19.             print(f"It took you {guesses_counter} guess(es).")
  20.             break
  21.         else:
  22.             cows = 0
  23.             bulls = 0
  24.             for index, k in enumerate(guess):
  25.                 if k == listnum[index]:
  26.                     bulls += 1
  27.                 elif k in listnum:
  28.                     cows += 1
  29.             print(f"Cows: {cows}, Bulls: {bulls}.")
  30.  
  31.  
  32. print("Please enter how many digits do you want the number to have?: ")
  33. num_digits = int(input())
  34. game(num_digits)
Advertisement
Add Comment
Please, Sign In to add comment