Help me with this cows and bulls game.. | SoloLearn: Learn to code for FREE!


Help me with this cows and bulls game..

while True: import random import re#regular expression num=str(random.randint(1000,9999)) #to generate random int num=re.findall('\S',num) #to split the random num into a list num=str(num) #convert it into a string print(num) inp=input('enter a number : ') inp=re.findall('\S',inp) #to split the input y=0 while y<=3: for n in num: if inp[y]==n: print([n]) break y+=1 #the loop to check for bulls which ain't workin... : (

5/22/2020 3:23:11 AM

Mons Joseph

2 Answers

New Answer


I had a play with it. You didn't need re. module. look at how i iterate through lists in mine compared to yours. Add the 'while True' loop only when you've got the mechanics working. You should be able to finish it off easier now. Remember - It's good that you used comments but comments go above what they describe. Use descriptive names for variables. Good luck!


#Help me with this cows and bulls game.. import random # bulls variable to record correct guesses bulls = 0 #to generate random int num=str(random.randint(1000,9999)) # Create list for random number numbers = [] # Add individual numbers to list for i in num: numbers.append(int(i)) print(numbers) # Get user guesses and add to list guess=input('enter a number : ') guesses = [] #to split the input for g in guess: guesses.append(int(g)) # Check the guesses against the random numbers position = 0 while position < 4: if guesses[position]==numbers[position]: bulls += 1 position += 1 # To go here: loop to check cows # Show bulls if bulls == 1: print(bulls,"bull") else: print(bulls,"bulls")