Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b8ee0c

Browse files
authored
Create Exercise-18-Cows-And-Bulls.py
1 parent a23792b commit 8b8ee0c

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

β€ŽExercise-18-Cows-And-Bulls.pyβ€Ž

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'''
2+
Exercise 18: Cows And Bulls
3+
4+
Create a program that will play the "cows and bulls"
5+
game with the user. The game works like this:
6+
7+
Randomly generate a 4-digit number. Ask the user to
8+
guess a 4-digit number. For every digit that the user
9+
guessed correctly in the correct place, they have a
10+
"cow". For every digit the user guessed correctly in
11+
the wrong place is a "bull." Every time the user makes
12+
a guess, tell them how many "cows" and "bulls" they
13+
have. Once the user guesses the correct number, the
14+
game is over. Keep track of the number of guesses the
15+
user makes throughout teh game and tell the user at
16+
the end.
17+
18+
Say the number generated by the computer is 1038. An
19+
example interaction could look like this:
20+
21+
Welcome to the Cows and Bulls Game!
22+
Enter a number:
23+
>>> 1234
24+
2 cows, 0 bulls
25+
>>> 1256
26+
1 cow, 1 bull
27+
...
28+
29+
Until the user guesses the number.
30+
31+
'''
32+
33+
# Solution
34+
def initiation():
35+
"""
36+
Set all the parameter needed.
37+
38+
Returns:
39+
guess_log -- a empty list container of all the guesses.
40+
answer -- a 4-digital number string of right answer.
41+
"""
42+
import random
43+
44+
print('Welcome to the Cows and Bulls Game!')
45+
guess_log = ['']
46+
answer = random.randint(0,9999)
47+
answer = '{0:04d}'.format(answer)
48+
return guess_log, answer
49+
50+
def get_guess(guess_log):
51+
"""
52+
Append a new guess to the previous log.
53+
54+
Arguments:
55+
guess_log -- a list contain all the guess(es).
56+
57+
Returns:
58+
guess_log -- a list contain all the guess(es).
59+
"""
60+
guess = input('Enter a number:')
61+
guess_log.append(guess)
62+
return guess_log
63+
64+
def compare(guess, answer):
65+
"""
66+
Compare guess and answer
67+
68+
Arguments:
69+
guess -- a 4-digital number string of the guess.
70+
answer -- a 4-digital number string of right answer.
71+
72+
Returns:
73+
cow -- a number of the user guessed correctly in the correct place.
74+
bull -- a number of the user guessed correctly in the wrong place.
75+
"""
76+
cow = 0
77+
bull = 0
78+
for i in range(len(guess)):
79+
if guess[i] == answer[i]:
80+
cow += 1
81+
if guess[i] in answer:
82+
bull += 1
83+
bull = bull - cow
84+
return cow, bull
85+
86+
def main():
87+
guess_log, answer = initiation()
88+
while guess_log[-1] != answer:
89+
guess_log = get_guess(guess_log)
90+
cow, bull = compare(guess_log[-1], answer)
91+
print('{} cows, {} bulls'.format(cow, bull))
92+
print('Your are right! After {} guess(es) you finally get it!\nYour logs:'.format(len(guess_log)-1), guess_log[1:])
93+
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /