-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess_the_word
57 lines (42 loc) · 1.84 KB
/
guess_the_word
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
import random # library that we use in order to choose on random words from a list of words
name = input("What is your name? ") # here the user is asked to enter the name first
print("Good Luck ! ", name)
words = ['beautiful','game','turing','ball','adya','computer','python','dog','cat','girl','IBAB','informatics','bangalore']
word = random.choice(words)# function will choose one random word from this list of words above
print("Guess the characters and type your answer in the box")
guesses = ''
#number of turns can be used
turns = 3
while turns > 0:
# counts the number of times of failure
failed = 0
# all characters from the input word taking one at a time.
for char in word:
# comparing that character with the character in guesses
if char in guesses:
print(char, end=" ")
else:
print("_")
print(char, end=" ")
# for every failure 1 will be incremented in failure
failed += 1
if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("You Win")
# this print the correct word
print("The word is: ", word)
break
# if user has input the wrong alphabet then it will ask user to enter another alphabet
print()
guess = input("guess a character:")
# every input character will be stored in guesses
guesses += guess
# check input with the character in word
if guess not in word:
turns -= 1
# if the character doesn’t match the word then “Wrong!” will be given as output
print("Wrong!")
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")