repl.it
@CarterMonkey/

Hangman game! Single Player and Multi Player!

Python

I have created a multiplayer and singleplayer hangman using Python!

fork
loading
Files
  • main.py
  • _test_runner.py
main.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import random
import getpass 

def random_word():
  words = ['Tom','Jerry','Robin','Starfire','BeastBoy','Cyborg','Raven','Daffy','Chewbacca','Tweety','Sylvester','CaptainKangaroo','Spike','BugsBunny','Gumball','Dora','Homer','Roadrunner','WildECoyote','Thor','Rocky','Bullwinkle','Ironman','CaptainAmerica','Superman','Flash','Aquaman','WonderWoman','Supergirl','Catwoman','Achilles','Aladdin','April','Apollo','Ariel','Arthur','Bambi','Bartholomew','Bear','Beast','Cinderella']
  return random.choice(words).upper()
  
print("Welcome to Hangman!")

modified_word = random_word()

def modified_start():
  global modified_word
  modified_word = random_word()
  rules()
  global guesses
  guesses = []
  secret = [] 
  global word
  for i in modified_word:
      secret.append(i)
  word_length = len(modified_word)
  char = ' '
  modified_word = modified_word.upper()
  global display
  display = char * word_length
  display = display.split(" ")
  display.pop()
  print("The word has:")
  print(word_length)
  print("Letters")

def modified_game():
  global errors
  global wrongGuess
  wrongGuess = []
  errors = 0
  modified_start()
  while True:
    print(wrongGuess)
    print(" ")
    print(display)
    print(errors)
    guessTurn(modified_word, wrongGuess)
    if guess in guesses:
      print("You've already guessed that letter!")
      errors -=0
      guessTurn(modified_word, wrongGuess)
    guesses.append(guess)
    if errors == 6:
      modified_loss() 
      False 
    if guess == modified_word:
      modified_won()
      False 
  begin()
  
def modified_guessTurn():
  global guess
  guess = input("What letter or word do you guess? ")
  guess = guess.upper()
  global wrong
  global errors
  wrong = "yes"
  print(" ")
  if guess.isalpha() == False:
    print("Letters only!")
    guessTurn(modified_word, wrongGuess)
  secret = []
  index = 0
  for i in modified_word:
    secret.append(i)
    if i == guess:
      display[index] = guess
      wrong = "no"
    index = index + 1
  modified_word = ''.join(secret)
  if guess == modified_word:
      display[index] = guess
      wrong = "no"
  if wrong == "yes":
    errors +=1
    wrongGuess.append(guess)
    print("You've gotten an error")
    print(" ")
    return errors
  
  
def rules():
  print(" ")
  print("In this game you will try to guess a secret word by guessing the letters in it.")
  print(" ")
  print("Once you know what the word is, instead of guessing a letter, guess the word.")
  print(" ")
  print('However, if you guess the wrong letter 6 times, you lose.')
  print(" ")
  
def start():
  rules()
  global errors
  global guesses
  guesses = []
  secret = [] 
  global word
  print(' ')
  print('Note to player typing in the word, it will not show you what youre typing in, it will still be there though')
  print(" ")
  word = getpass.getpass(prompt = "What is your secret word? ")
  if word.isalpha() == False:
    print("Please try a word without numbers or symbols ")
    start()
  for i in word:
      secret.append(i)
  word_length = len(word)
  char = '__ '
  word = word.upper()
  global display
  display = char * word_length
  display = display.split(" ")
  display.pop()
  print("The word has:")
  print(word_length)
  print("Letters")
def modified_won():
  print("Congratulations! You've won! The word was " + modified_word)
  print(" ")
  print("Welcome back to another game of Hangman!")
  begin()
  
def won():
  print("Congratulations! You've won! The word was " + word)
  print(" ")
  print("Welcome back to another game of Hangman!")
  begin()

def loss():
  print("Sorry you've lost, the word was " + word)
  print(' ')
  print("The six incorrect letters you guessed were: ")
  print(' ') 
  print(wrongGuess)
  print(" ")
  print("Welcome back to another game of Hangman! Hopefully you will win this time!")
  begin()

def modified_loss():
  print("Sorry you've lost, the word was " + modified_word)
  print("The six incorrect letters you guessed were: ")
  print(wrongGuess)
  print(" ")
  print("Welcome back to another game of Hangman! Hopefully you will win this time!")
  begin()

def guessTurn(word, wrongGuess):
  global guess
  guess = input("What letter or word do you guess? ")
  guess = guess.upper()
  global wrong
  global errors
  wrong = "yes"
   
  if guess.isalpha() == False:
    print("Letters only!")
    guessTurn(word, wrongGuess)
  secret = []
  index = 0
  for i in word:
    secret.append(i)
    if i == guess:
      display[index] = guess
      wrong = "no"
    index = index + 1
  word = "".join(secret)
  if guess == word:
    wrong = "no"
  if wrong == "yes":
    errors +=1
    wrongGuess.append(guess)
    print("You've gotten an error")
    print(" ")
    return errors

def begin():
  singleList = ["singleplayer","single","s"]
  multiList = ["multiplayer", "multi", "m"]
  question = input("Before we begin, would you like to play multiplayer or singleplayer? ")
  print(" ")
  if question.lower() in singleList:
    print("All of these will be characters from movies or cartoons!")
    modified_game()
  elif question.lower() in multiList:
    print("Ok")
    game()
  else:
    print('Please answer multiplayer or singleplayer')
    print(" ")
    begin()
    
  question = question.upper()
  


def game():
  global errors
  global wrongGuess
  wrongGuess = []
  errors = 0
  start()
  while True:
    print(wrongGuess)
    print(display)
    print(errors)
    guessTurn(word, wrongGuess)
    if guess in guesses:
      print("You've already guessed that letter!")
      errors -=0
      guessTurn(word, wrongGuess)
    guesses.append(guess)
    if errors == 6:
      loss()
      False
    if guess == word:
      won()
      False
  begin()
 
begin()