-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_18_cows_and_bulls.py
145 lines (111 loc) · 5.54 KB
/
exercise_18_cows_and_bulls.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# coding=utf-8
"""
Cows And Bulls
Exercise 18 (and Solution)
Create a program that will play the “cows and bulls” game with the user. The game works like this:
Randomly generate a 4-digit number. Ask the user to guess a 4-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,
tell them how many “cows” and “bulls” they have. Once the user guesses the correct number,
the game is over. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.
Say the number generated by the computer is 1038. An example interaction could look like this:
Welcome to the Cows and Bulls Game!
Enter a number:
>>> 1234
2 cows, 0 bulls
>>> 1256
1 cow, 1 bull
...
Until the user guesses the number.
Concepts for this week:
Randomness (we’ve covered this a few times before. Mainly in a previous exercise.)
Functions (covered in a previous exercise also)
Main method
Main method
Since we have covered randomness and functions, we still need to cover
the idea of a “main” method. If you have programmed before, you will wonder why you haven’t needed
a main method so far in your Python code.
First, here is how you do it:
def square(num):
return num * num
if __name__=="__main__":
user_num = input("Give me a number: ")
print(square(num))
Note that in both __name__ and __main__, there are TWO underscore (_) characters.
If you run this program, it behaves as expected: asking the use for a number and printing
out the square in return. However, you might be wondering - how is this different
from the way we’ve been writing programs until this point?
def square(num):
return num * num
user_num = input("Give me a number: ")
print(square(num))
You are correct if you think the second example will have THE SAME BEHAVIOR as
in the first case. At least the way we’ve been running Python files until this point. I will attempt to explain.
Python does not have a specified entry point like many other languages
(for example, C++ always looks for a void main() method to run).
Python files and programs are executed line by line by the Python interpreter,
from however the Python file or program is run. When the definition of a function is reached
by the interpreter, the function is not run, but rather loaded to be run later.
(We discussed this when we talked about functions in a previous post.)
Whatever is written outside of a function will get executed immediately -
this includes creating variables and calling the functions that were previously loaded.
There are (most commonly) two ways to run a Python file:
“Just running it” (which you can do from the terminal, through the “Run” button in IDLE, etc.)
Importing the file as a module
The first way is the way that you most likely have been using if you have been
doing the exercises in this blog. If you have many files in a large project, this
is how you run the “entry point” file in the project.
The second way is more subtle: it is what happens when you write an import statement
at the top of your file. In this exercise for example, you need to import random into
your program to use the random module. Somewhere on your computer there is a file
(or a group of Python files) that make up the random module, and in the process
of importing them, what you are actually doing is running the file(s) from that module.
When you have functions defined inside a file (with the def keyword, like the def
square function above) and run a file, the function is not immediately run.
You can think what happens is the function is stored for future use.
Anything else in the file - variables created, functions that are called,
operations done, etc - are executed immediately when a Python file is run.
However, in the case where you are importing a Python file into another,
you want to load all of the functions without creating variables or executing
any functions. You just want to load them to use them later. How do you
reconcile this problem? With the if __name__=="__main__" statement.
Create your variables and all your functions inside this statement,
and when you import your file into another, it will not mess up your
program by creating variables or calling functions.
What the if __name__=="__main__" statement from above does is ensure that variables
that are created, functions that are called, operations that are done, etc ONLY
when you directly run the file, not when you import it into another.
"""
import random
def kereses():
t=str(tipp)
n=str(szam)
cow_num=0
bull_num=0
for i in range(4):
if t[i]==n[i]:
cow_num+=1
else:
if t[i] in n:
bull_num+=1
print("{} cows, {} bulls".format(cow_num, bull_num))
if __name__ == "__main__":
szam = random.randint(1000, 9999)
print("""
Gondoltam egy négyjegyű számra!
Próbálja meg kitalálni.
Ha eltalál egy számot, ami az általam gondolt számban is szerepel, akkor a 'bull' szöveg fog megjelenni.
Ha a szám helyét is eltalálta a számban, akkor a 'cow' szöveg.
Sok sikert!
""")
print(szam)
tippszam = 0
tipp=0
ujra="i"
while ujra.lower()=="i"
while szam!=tipp:
tippszam+=1
tipp = int(input("Melyik számra gondoltam? "))
kereses()
print("Kitaláltad, gratulálok!")
ujra=input("Akarsz újra játszani i/n")