-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day7.py
91 lines (71 loc) · 2.33 KB
/
Day7.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
Created on Wed May 26 15:47:39 2021
@author: Tahamid
"""
# =============================================================================
# Day 7
# =============================================================================
# BUILD A VERSION OF YOUR OWN HANGMAN GAME
import random
import Day7utils
logo = Day7utils.logo
stages = Day7utils.stages
lb = Day7utils.lineBreak
word_list = Day7utils.word_list
word = random.choice(word_list)
lives = 6
# initialize a list using
# listName = [initValue] * listSize
display = ['_'] * len(word)
# =============================================================================
# we can also initiate using a loop
#
# for i in range(len(word)):
# display += '_'
# =============================================================================
print(logo)
print("\n")
print(display)
# =============================================================================
# gameOver = False
# while gameOver:
# take guess
# for loop:
# if letter matches guess:
# change the display's element in that index to guess
# print display
# if "_" not in display:
# gameOver = True
# =============================================================================
while display.count('_') != 0 and lives != 0:
guess = input("\nGuess a letter: ").lower()
print(lb)
# enumerate keeps track of both index and value and we can start enumerating
# from any position in the iterable
for index, letter in enumerate(word):
if letter == guess:
display[index] = guess
if guess not in word:
lives -= 1
print(stages[lives])
print(display)
# =============================================================================
# we can also use a simple for loop instead of enumerate
# for i in range(len(word)):
# letter = word[i]
# if letter == guess:
# display[i] = guess
# else:
# print("Wrong")
# =============================================================================
if lives == 0:
print(f"\n{lb}")
print(f"\nThe word was {word}.")
print("You lose!")
else:
print(f"\n{lb}")
print("\nYou win!")
# =============================================================================
# End of Day 7
# =============================================================================