FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-beginner-projects/projects/Mastermind/main.py at main · BrianCodeLab/python-beginner-projects · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
BrianCodeLab
/
python-beginner-projects
Public
forked from
Mrinank-Bhowmick/python-beginner-projects
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-beginner-projects
/
projects
/
Mastermind
/
main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (52 loc) · 2.08 KB
Breadcrumbs
python-beginner-projects
/
projects
/
Mastermind
/
main.py
Copy path
File metadata and controls
64 lines (52 loc) · 2.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
random
num
=
random
.
randrange
(
1000
,
10000
)
n
=
int
(
input
(
"Guess the 4 digit number:"
))
# guess made. Program terminates if true.
if
n
==
num
:
print
(
"Great! You guessed the number in just 1 try! You're a Mastermind!"
)
else
:
# ctr variable initialized. It will keep count of
# the number of tries the Player takes to guess the number.
ctr
=
0
# while loop repeats as long as the
# Player fails to guess the number correctly.
while
n
!=
num
:
# variable increments every time the loop
# is executed, giving an idea of how many
# guesses were made.
ctr
+=
1
count
=
0
# explicit type conversion of an integer to
# a string in order to ease extraction of digits
n
=
str
(
n
)
# explicit type conversion of a string to an integer
num
=
str
(
num
)
# correct[] list stores digits which are correct
correct
=
[
"X"
]
*
4
# for loop runs 4 times since the number has 4 digits.
for
i
in
range
(
0
,
4
):
# checking for equality of digits
if
n
[
i
]
==
num
[
i
]:
# number of digits guessed correctly increments
count
+=
1
# hence, the digit is stored in correct[].
correct
[
i
]
=
n
[
i
]
else
:
continue
# when not all the digits are guessed correctly.
if
(
count
<
4
)
and
(
count
!=
0
):
print
(
"Not quite the number. But you did get "
,
count
,
" digit(s) correct!"
)
print
(
"Also these numbers in your input were correct."
)
for
k
in
correct
:
print
(
k
,
end
=
" "
)
print
(
"
\n
"
)
print
(
"
\n
"
)
n
=
int
(
input
(
"Enter your next choice of numbers: "
))
# when none of the digits are guessed correctly.
elif
count
==
0
:
print
(
"None of the numbers in your input match."
)
n
=
int
(
input
(
"Enter your next choice of numbers: "
))
# condition for equality.
if
n
==
num
:
print
(
"You've become a Mastermind!"
)
print
(
"It took you only"
,
ctr
,
"tries."
)
Back
|
FazBrowse Home
|
New Git URL