Skip to content

Commit c0c6aeb

Browse files
Create word guessing game
we will use the random module to make a word-guessing game. This game is for beginners learning to code in python and to give them a little brief about using strings, loops, and conditional(If, else) statements.
1 parent bb5d51b commit c0c6aeb

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

W/word guessing game

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import random
2+
# library that we use in order to choose
3+
# on random words from a list of words
4+
5+
name = input("What is your name? ")
6+
7+
# Here the user is asked to enter the name first
8+
9+
print("Good Luck ! ", name)
10+
11+
words = ['rainbow', 'computer', 'science', 'programming',
12+
'python', 'mathematics', 'player', 'condition',
13+
'reverse', 'water', 'board', 'geeks']
14+
15+
# Function will choose one random
16+
# word from this list of words
17+
word = random.choice(words)
18+
19+
20+
print("Guess the characters")
21+
22+
guesses = ''
23+
24+
# any number of turns can be used here
25+
turns = 12
26+
27+
28+
while turns > 0:
29+
30+
# counts the number of times a user fails
31+
failed = 0
32+
33+
# all characters from the input
34+
# word taking one at a time.
35+
for char in word:
36+
37+
# comparing that character with
38+
# the character in guesses
39+
if char in guesses:
40+
print(char, end=" ")
41+
42+
else:
43+
print("_")
44+
45+
# for every failure 1 will be
46+
# incremented in failure
47+
failed += 1
48+
49+
if failed == 0:
50+
# user will win the game if failure is 0
51+
# and 'You Win' will be given as output
52+
print("You Win")
53+
54+
# this print the correct word
55+
print("The word is: ", word)
56+
break
57+
58+
# if user has input the wrong alphabet then
59+
# it will ask user to enter another alphabet
60+
print()
61+
guess = input("guess a character:")
62+
63+
# every input character will be stored in guesses
64+
guesses += guess
65+
66+
# check input with the character in word
67+
if guess not in word:
68+
69+
turns -= 1
70+
71+
# if the character doesn’t match the word
72+
# then “Wrong” will be given as output
73+
print("Wrong")
74+
75+
# this will print the number of
76+
# turns left for the user
77+
print("You have", + turns, 'more guesses')
78+
79+
if turns == 0:
80+
print("You Loose")

0 commit comments

Comments
 (0)