Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Group Project
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ahmed Yusuf
Group Project
Commits
f95ea056
Commit
f95ea056
authored
2 years ago
by
Callum Richardson
Browse files
Options
Downloads
Patches
Plain Diff
hangman file
parent
d010e132
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Group_game/hangman.py
+82
-0
82 additions, 0 deletions
Group_game/hangman.py
with
82 additions
and
0 deletions
Group_game/hangman.py
0 → 100644
+
82
−
0
View file @
f95ea056
Python
3.10
.
8
(
tags
/
v3
.
10.8
:
aaaf517
,
Oct
11
2022
,
16
:
50
:
30
)
[
MSC
v
.
1933
64
bit
(
AMD64
)]
on
win32
Type
"
help
"
,
"
copyright
"
,
"
credits
"
or
"
license()
"
for
more
information
.
>>>
...
from
collections
import
Counter
...
...
word
=
"
closet
"
...
...
def
hangman
(
word
):
...
print
(
'
Guess the word! HINT: you may find this thing in your bedroom
'
)
...
print
(
"
You will have 15 chances to guess the word.
"
)
...
...
for
i
in
word
:
...
# For printing the empty spaces for letters of the word
...
print
(
'
_
'
,
end
=
'
'
)
...
print
()
...
...
playing
=
True
...
# list for storing the letters guessed by the player
...
letterGuessed
=
''
...
chances
=
15
...
correct
=
0
...
flag
=
0
...
try
:
...
while
(
chances
!=
0
)
and
flag
==
0
:
#flag is updated when the word is correctly guessed
...
print
()
...
print
(
"
you have
"
+
str
(
chances
)
+
"
guesses left!
"
)
...
chances
-=
1
...
...
try
:
...
guess
=
str
(
input
(
'
Enter a letter to guess:
'
))
...
except
:
...
print
(
'
Enter only a letter!
'
)
...
continue
...
...
# Validation of the guess
...
if
not
guess
.
isalpha
():
...
print
(
'
Enter only a LETTER
'
)
...
continue
elif
len
(
guess
)
>
1
:
print
(
'
Enter only a SINGLE letter
'
)
continue
elif
guess
in
letterGuessed
:
print
(
'
You have already guessed that letter
'
)
continue
# If letter is guessed correctly
if
guess
in
word
:
k
=
word
.
count
(
guess
)
#k stores the number of times the guessed letter occurs in the word
for
_
in
range
(
k
):
letterGuessed
+=
guess
# The guess letter is added as many times as it occurs
# Print the word
for
char
in
word
:
if
char
in
letterGuessed
and
(
Counter
(
letterGuessed
)
!=
Counter
(
word
)):
print
(
char
,
end
=
'
'
)
correct
+=
1
# If user has guessed all the letters
elif
(
Counter
(
letterGuessed
)
==
Counter
(
word
)):
# Once the correct word is guessed fully,
# the game ends, even if chances remain
print
(
"
The word is:
"
,
end
=
'
'
)
print
(
word
)
flag
=
1
print
(
'
Congratulations, You won!
'
)
break
# To break out of the for loop
break
# To break out of the while loop
else
:
print
(
'
_
'
,
end
=
'
'
)
# If user has used all of his chances
if
chances
<=
0
and
(
Counter
(
letterGuessed
)
!=
Counter
(
word
)):
print
()
print
(
'
You lost! Try again..
'
)
print
(
'
The word was {}
'
.
format
(
word
))
except
KeyboardInterrupt
:
print
()
print
(
'
Bye! Try again.
'
)
exit
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment