Advertisement
Sb93

Bulls and Cows Python

Oct 7th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | Source Code | 0 0
  1. import random
  2. attempts = 0
  3.  
  4. while True:
  5.     num = str(random.randrange(1000,10000))
  6.     count = [num.count(i) for i in num if num.count(i)>1]
  7.     if not count: break
  8.  
  9. while True:
  10.     guess = input()
  11.     bulls, cows = 0,0
  12.     attempts += 1
  13.  
  14.     if [guess.count(j) for j in guess if guess.count(j)>1]:
  15.         print("Duplicate number entered, please try again")
  16.         continue
  17.  
  18.     for idx,dgt in enumerate(guess):
  19.  
  20.         if dgt == num[idx]:
  21.             bulls+=1
  22.             continue
  23.  
  24.         elif dgt in num:
  25.             cows+=1
  26.             continue
  27.    
  28.     if bulls==4: print(f"Number : {num}\nAttempts : {attempts}")
  29.     else: print(f"{bulls}A {cows}B")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement