1import random
2stages = ['''
3 +---+
4 | | You hanged the man
5 | | ____________
6 O | | \/ \/ |
7 /|\ | | /\ /\ |
8 / \ | | * |
9 | ------ |
10========= |____________|
11''', '''
12 +---+
13 | |
14 O |
15 /|\ |
16 / |
17 |
18=========
19''', '''
20 +---+
21 | |
22 O |
23 /|\ |
24 |
25 |
26=========
27''', '''
28 +---+
29 | |
30 O |
31 /| |
32 |
33 |
34=========''', '''
35 +---+
36 | |
37 O |
38 | |
39 |
40 |
41=========
42''', '''
43 +---+
44 | |
45 O |
46 |
47 |
48 |
49=========
50''', '''
51 +---+
52 | |
53 |
54 |
55 |
56 |
57=========
58''']
59words = ["in","apple","mango","good","india","priyanka","gramamr"]
60computer_choice = random.choice(words)
61
62
63display = []
64word_length = len(computer_choice)
65for _ in range(word_length):
66 display += "_"
67print(display)
68The_end = False
69lives = 6
70while not The_end:
71 if lives == 6:
72 print(stages[6])
73 guess = input("Guess a letter: ").lower()
74 for position in range(word_length):
75 letter = computer_choice[position]
76 if letter == guess:
77 display[position] = letter
78
79 print(display)
80 if guess not in computer_choice:
81 lives -= 1
82 if lives == 0:
83 print("you lost")
84 print(stages[0])
85 The_end = True
86 elif lives == 1:
87 print(stages[1])
88 elif lives == 2:
89 print(stages[2])
90 elif lives == 3:
91 print(stages[3])
92 elif lives == 4:
93 print(stages[4])
94 elif lives == 5:
95 print(stages[5])
96 if "_" not in display:
97 The_end = True
98 print("You won")
1
2#importing the time module
3import time
4
5#welcoming the user
6name = raw_input("What is your name? ")
7
8print "Hello, " + name, "Time to play hangman!"
9
10print "
11"
12
13#wait for 1 second
14time.sleep(1)
15
16print "Start guessing..."
17time.sleep(0.5)
18
19#here we set the secret
20word = "secret"
21
22#creates an variable with an empty value
23guesses = ''
24
25#determine the number of turns
26turns = 10
27
28# Create a while loop
29
30#check if the turns are more than zero
31while turns > 0:
32
33 # make a counter that starts with zero
34 failed = 0
35
36 # for every character in secret_word
37 for char in word:
38
39 # see if the character is in the players guess
40 if char in guesses:
41
42 # print then out the character
43 print char,
44
45 else:
46
47 # if not found, print a dash
48 print "_",
49
50 # and increase the failed counter with one
51 failed += 1
52
53 # if failed is equal to zero
54
55 # print You Won
56 if failed == 0:
57 print "
58You won"
59
60 # exit the script
61 break
62
63 print
64
65 # ask the user go guess a character
66 guess = raw_input("guess a character:")
67
68 # set the players guess to guesses
69 guesses += guess
70
71 # if the guess is not found in the secret word
72 if guess not in word:
73
74 # turns counter decreases with 1 (now 9)
75 turns -= 1
76
77 # print wrong
78 print "Wrong
79"
80
81 # how many turns are left
82 print "You have", + turns, 'more guesses'
83
84 # if the turns are equal to zero
85 if turns == 0:
86
87 # print "You Lose"
88 print "You Lose
89"
90