-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython hangman.py
More file actions
119 lines (68 loc) · 2.75 KB
/
Copy pathPython hangman.py
File metadata and controls
119 lines (68 loc) · 2.75 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#Made By Blake McCullough
#Discord - Spoiled_Kitten#4911
#Github - https://github.com/Blake-McCullough/
#Email - privblakemccullough@protonmail.com
import random
#variables for word lists
words=["cat","chicken","dog"]
#prints creators name
print('Hangman By Blake McCullough')
#asks for name
name=input("What is your name?")
#capitalizes first letter of word and decapitalize other letters for name
name = name.capitalize()
#says a greetings
print("Hello", name, "Hope you enjoy!")
#word guessing system
def get_guess():
# Set the dashes to the length of the secret word and set the amount of guesses
# the user has to 10
dashes = "-" * len(secret_word)
guesses_left = 10
# This will loop as long as BOTH conditions are true:
# 1. The number of guesses of left is greater than -1
# 2. The dash string does NOT equal the secret word
while guesses_left > -1 and not dashes == secret_word:
# Print the amount of dashes and guesses left
print(dashes)
print("guesses left:")
print (str(guesses_left))
# Ask the user for input
guess = input("Guess:")
# Conditions that will print out a message according to
# invalid inputs
if len(guess) != 1:
print ("Your guess must have exactly one character!")
# If the guess is in the secret word then we updtae dashes to replace the
# corresponding dash with the correct index the guess belongs to in the
# secret word
elif guess in secret_word:
print ("That letter is in the secret word")
dashes = update_dashes(secret_word, dashes, guess)
# If the guess is wrong then we display a message and subtract
# the amount of guesses the user has by 1
else:
print ("That letter is not in the secret word!")
guesses_left -= 1
if guesses_left < 0:
print ("You lose you. The word was: " + str(secret_word))
# If the dash string equals the secret word in the end then the
# user wins
else:
print ("Congrats! You Win. The word was: " + str(secret_word))
# This function updates the string of dashes by replacing the dashes
# with words that match up with the hidden word if the user manages to guess
# it correctly
def update_dashes(secret, cur_dash, rec_guess):
result = ""
for i in range(len(secret)):
if secret[i] == rec_guess:
result = result + rec_guess # Adds guess to string if guess is correctly
else:
# Add the dash at index i to result if it doesn't match the guess
result = result + cur_dash[i]
return result
# Words that can be guessed
secret_word = random.choice(words)
get_guess()
print('Thanks', name,'For Playing!')