-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcowsbull.py
38 lines (31 loc) · 1.33 KB
/
cowsbull.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import random
def compare_numbers(number, user_guess):
cowbull = [0,0] #cows, then bulls
for i in range(len(number)):
if number[i] == user_guess[i]:
cowbull[1]+=1
else:
cowbull[0]+=1
return cowbull
if __name__=="__main__":
playing = True #gotta play the game
number = str(random.randint(0,9999)) #random 4 digit number
guesses = 0
print("Let's play a game of Cowbull!") #explanation
print("I will generate a number, and you have to guess the numbers one digit at a time.")
print("For every number in the wrong place, you get a cow. For every one in the right place, you get a bull.")
print("The game ends when you get 4 bulls!")
print("Type exit at any prompt to exit.")
while playing:
user_guess = input("Give me your best guess!")
if user_guess == "exit":
break
cowbullcount = compare_numbers(number,user_guess)
guesses+=1
print("You have "+ str(cowbullcount[0]) + " cows, and " + str(cowbullcount[1]) + " bulls.")
if cowbullcount[1]==4:
playing = False
print("You win the game after " + str(guesses) + "! The number was "+str(number))
break #redundant exit
else:
print("Your guess isn't quite right, try again.")