You are on page 1of 5

Task 2 (Hangman)

Variables Data structures


name While loop
word For loop
words If – else if
guess
guesses
turns
failed Pseudo Code
char
1. Import random
2. Start function hangman()
3. Restart = “Y”
4. Start while not in (‘n’, ‘No’, ‘no’, ‘N’)
5. Input (“what is your name”)
6. Print (“Good Luck”, name)
7. Store values in a variable words
8. Assign the function to a variable word
word = random.choices(words)
9. Print (“Guess the characters”)
10. Initialize variable guesses = ’ ‘
11. Initialize variable turns = 12
12. Start while turns > 0
13. Initialize variable failed = 0
14. Start For loop to find a character in a word
15. Use IF(char in word)
16. Print (char) else
17. Print (“_”, end=” “)
18. Increment variable failed one time
failed+=1
19. Check condition IF(failed == 0)
20. Print (“You Win”)
21. Print the correct word
print(“the word is: ”,word)

22. Restart the game


restart = input(“Do you want to play again”)
23. Break the statement
24. Guess a character by using input
guess=input(”Guess a character”)
25. Increment the variable guesses
guesses+ = guess
26. Check condition IF(guess not in word)
27. Decrement the value of turn one time
turns- = turns
28. Print (“Wrong”)
29. Print (“You have”, +turns, “more guesses”)
30. End of loop
31. Check the condition IF(turns == 0)
32. Print (“You Lose”)
33. Restart the game by inputting “Y”
Restart = input(“You want to play”)
34. End of hangman()

Python Code
import random

def hangman():
restart = ('Y')
while restart not in ('n', 'NO', 'no', 'N'):
name = input("What is your name? ")

print("Good Luck ! ", name)

words = ['rainbow',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board']

word = random.choice(words)

print("Guess the characters")

guesses = ''

turns = 12

while turns >0:


failed = 0

for char in word:

if char in guesses:
print(char)

else:
print("_",end=" ")

failed += 1

if failed == 0:

print("You Win")
restart = input("You want to play Again")

# this print the correct word


print("The word is: ", word)
break

guess = input("guess a character:")

guesses += guess

if guess not in word:

turns -= 1

print("Wrong")

print("You have", + turns, 'more guesses')

if turns == 0:
print("You Lose")
restart = input("You want to play")

hangman()
Output
Summary

The program for this task is about hangman game which stores some words and let the user
play the game by guessing the characters within a word. The game starts with the message to
ask user name. User will enter the name and the game print the user name with a “Good Luck”
message and ask the user to guess the character in a word by inputting a character. If the
guessed character is found in a word, then program asks the user to enter/guess another
character. If the character is wrong the program display “Wrong” and also display the
remaining guesses. In this game user have 12 guesses to attempt and every time when the
guess is wrong it will subtract one value from total turns.
By using this structure, program uses for and while loop to repeat and
increment/decrement the values of certain variables which are already defined in pseudo and
python code.
If the user guess all the characters in a word correct then program displays a message
“You Win” and when all the guesses are wrong then program displays a message “You Lose”. If
the user wants to play again the program restart the game by let the user to input ‘Y’ with a
input message “You want to play”, then end of hangman() function and end of main() function.

You might also like