Cows and Dulls Write a program called q3py that plays the Co

Cows and Dulls. Write a program (called q3.py) that plays the Cows and Bulls game. The game works as follows. Randomly generate a 3-digit number, where every digit between 0 and 9 is unique. Ask the user to guess a 3-digit number. For every digit that the user guessed correctly in the correct place, they have a \"cow\". For every digit the user guessed correctly in the wrong place is a \"bull.\" Every time the user makes a guess, report the number of \"cows\" and \"bulls\". Once the user guesses the correct number, the game is over. q3.py has been provided for you. It contains two user-defined functions: main() and welcome(). The main() function drives the program. The welcome () function prints a welcome message to the screen. The code for the welcome () function has been provided for you. Your goal is to write the remaining part of the main() function. Programming tips. When generating a 3-digit random number, make sure that every digit is unique. The best way to do this to keep generating a random number until the digits are unique. Note that since the program uses random number, your program will not run in the same manner as the following examples unless the same random numbers are generated. Once the welcome message printed (lines 1-3), the user initially guesses 123 (line 5). The computer reports 0 cows and 0 bulls (line 6). The user then guesses 456 at the prompt (line 8), and the computer reports 1 cows, 2 bulls (line 9). The user guesses 465 for the third guess (line 11). The computer reports 0 cows, 3 bulls (line 12). The user keeps playing the game until the number is guessed correctly (lines 14-18). A congratulations message is printed (line 20) along with the secret number and number of guesses (line 21).

Solution

import random
def welcome():
print(\'-----------------------------\')
print( \'| Welcome to Cows and Bulls |\')
print( \'-----------------------------\')
print(\'\')

def main():
welcome()
lis = [0,1,2,3,4,5,6,7,8,9]
n = []
temp = random.randrange(len(lis))
n.append(lis[temp])
lis = lis[0:temp] + lis[temp+1:]

temp = random.randrange(len(lis))
n.append(lis[temp])
lis = lis[0:temp] + lis[temp+1:]

temp = random.randrange(len(lis))
n.append(lis[temp])
lis = lis[0:temp] + lis[temp+1:]

#print(n)
ss = \"\".join(str(eg) for eg in n)

count = 1
cow = 0
bull = 0
while(cow!=3):
   #print(),
   cow = 0
   bull = 0
   inp = int(raw_input(\'Guess #\'+str(count)+\':\'))
   inp_list = []
   inp_list.append(inp%10)
   inp = inp/10
   inp_list.append(inp%10)
   inp = inp/10
   inp_list.append(inp%10)
   inp = inp/10
   inp_list.reverse()
   #print(inp_list)
   for i in range(0,3):
       if(inp_list[i] in n):
           if(inp_list[i]==n[i]):
               cow = cow + 1
           else:
               bull = bull + 1
   print(str(cow) + \' cows, \'+str(bull)+\' bulls.\')
   count = count + 1
count = count - 1
print(\'You got it!\')
print(\'It took you \'+str(count)+\' guesses to find the secret number \'+ss+\'.\')  
# Write the rest of code here.   

main()

 Cows and Dulls. Write a program (called q3.py) that plays the Cows and Bulls game. The game works as follows. Randomly generate a 3-digit number, where every d
 Cows and Dulls. Write a program (called q3.py) that plays the Cows and Bulls game. The game works as follows. Randomly generate a 3-digit number, where every d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site