• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Hangman (original)

uottawamakermobile Apr 7th, 2020 217 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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. # Here the user is asked to enter the name first
7.
8. print("Good Luck ! ", name)
9.
10. words = ['rainbow', 'computer', 'science', 'programming',
11.         'python', 'mathematics', 'player', 'condition',
12.         'reverse', 'water', 'board', 'geeks']
13.
14. # Function will choose one random
15. # word from this list of words
16. word = random.choice(words)
17.
18.
19. print("Guess the characters")
20.
21. guesses = ''
22.
23. # any number of turns can be used here
24. turns = 12
25.
26.
27. while turns > 0:
28.
29.     # counts the number of times a user fails
30.     failed = 0
31.
32.     # all characters from the input
33.     # word taking one at a time.
34.     for char in word:
35.
36.         # comparing that character with
37.         # the character in guesses
38.         if char in guesses:
39.             print(char)
40.
41.         else:
42.             print("_")
43.
44.             # for every failure 1 will be
45.             # incremented in failure
46.             failed += 1
47.
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.     guess = input("guess a character:")
61.
62.     # every input character will be stored in guesses
63.     guesses += guess
64.
65.     # check input with the character in word
66.     if guess not in word:
67.
68.         turns -= 1
69.
70.         # if the character doesn’t match the word
71.         # then “Wrong” will be given as output
72.         print("Wrong")
73.
74.         # this will print the number of
75.         # turns left for the user
76.         print("You have", + turns, 'more guesses')
77.
78.
79.         if turns == 0:
80.             print("You Loose")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top