Reputation: 13
This code is for a hangman game and it works perfectly.It, however, is quite messy and long because I had to copy and paste a lot of the code over and over again. It works but is not the most functional way of doing this. How can I clean up this code to not have to copy and paste over and over again? I already tried using a for loop and a while loop but that just messed up the game and the game then didn't work properly. How can I improve this code?
import random
def getWord():
dictionary_file=open("dictionary.txt","r")
lines=dictionary_file.readlines()
words=[]
for i in range(0,len(lines)-1):
x=lines[i]
z=len(x)
a=x[:z-1]
words.append(a)
words.append(lines[i+1])
game_word=random.choice(words)
dictionary_file.close()
return(game_word)
def display(game_word):
for i in range(len(game_word)):
print('_ ', end='')
print()
def play(game_word):
n = len(game_word)
word = list("_"*n)
guess1 = input("Letter: ")
if guess1.isalpha():
for j in range(n):
if guess1 in game_word[j]:
word[j] = guess1 + " "
else:
word[j] = "_ "
print("".join(word))
guess2 = input("Letter: ")
if guess2.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess2 in game_word[j]:
word[j] = guess2 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess3 = input("Letter: ")
if guess3.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess3 in game_word[j]:
word[j] = guess3 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess4 = input("Letter: ")
if guess4.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess4 in game_word[j]:
word[j] = guess4 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess5 = input("Letter: ")
if guess5.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess5 in game_word[j]:
word[j] = guess5 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
return
guess6 = input("Letter: ")
if guess6.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess6 in game_word[j]:
word[j] = guess6 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess7 = input("Letter: ")
if guess7.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess7 in game_word[j]:
word[j] = guess7 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess8 = input("Letter: ")
if guess8.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess8 in game_word[j]:
word[j] = guess8 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess9 = input("Letter: ")
if guess9.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess9 in game_word[j]:
word[j] = guess9 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess10 = input("Letter: ")
if guess10.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess10 in game_word[j]:
word[j] = guess10 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess11 = input("Letter: ")
if guess11.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess11 in game_word[j]:
word[j] = guess11 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess12 = input("Letter: ")
if guess12.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess12 in game_word[j]:
word[j] = guess12 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess13 = input("Letter: ")
if guess13.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess13 in game_word[j]:
word[j] = guess13 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess14 = input("Letter: ")
if guess14.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess14 in game_word[j]:
word[j] = guess14 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
guess15 = input("Letter: ")
if guess15.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess15 in game_word[j]:
word[j] = guess15 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
print("YOU LOSE")
print('Word was '+ game_word)
game_word = getWord()
display(game_word)
play(game_word)
Upvotes: 1
Views: 73
Reputation: 16
import random
def getWord():
dictionary_file=open("dictionary.txt","r")
lines=dictionary_file.readlines()
words=[]
for i in range(0,len(lines)-1):
x=lines[i]
z=len(x)
a=x[:z-1]
words.append(a)
words.append(lines[i+1])
solution_word=random.choice(words)
dictionary_file.close()
return(solution_word)
def print_in_game(word):
for i in range(len(word)):
print(word[i], end=' ')
print()
def play(solution_word):
n = len(solution_word)
word = list("_"*n)
print_in_game(word)
for index in range(15):
guess = input("Letter: ")
if not guess.isalpha():
print("try letters please")
continue
for character_index in range(n):
if word[character_index] == '_':
if guess == solution_word[character_index]:
word[character_index] = guess + " "
else:
word[character_index] = "_"
print_in_game(word)
if '_' not in word:
print("YOU WIN")
return
print("YOU LOSE")
print('Word was '+ solution_word)
solution_word = getWord()
play(solution_word)
I hope it helps. It still can be improved with better variable names, and maybe the play function can be split to smaller functions it still seems a little long.
Upvotes: 0
Reputation: 27557
You can use enumerate to tidy up the range part in the for-loop, and with enumerate, instead of keep using subscriptions to get a value from the list, simple use the second variable.
That means, (i, lines[i])
turns into (i, v)
.
def getWord():
with open("dictionary.txt","r") as dictionary_file: # Context manager are a better practice
lines=dictionary_file.readlines()
words=[]
for i,v in enumerate(lines): # Enumerate will allow you to use the index of an iteration along-side the corresponding value
words.append(v[:len(v)-1])
words.append(v)
game_word=random.choice(words)
return(game_word)
Upvotes: 1
Reputation: 23149
And in your case, it will be very simple.
Obviously, as you noticed, your code is largely a repetition of the same (or very similar) instructions.
For instance :
guess10 = input("Letter: ")
if guess10.isalpha():
for j in range(n):
if word[j] == '_ ':
if guess10 in game_word[j]:
word[j] = guess10 + " "
else:
word[j] = "_ "
print("".join(word))
if '_ ' not in word:
print("YOU WIN")
return
Is an example of the "pattern" that is repeated. The only difference is the number 10
in the variable name, because that's the 10's time that this code is executed.
Look at the definition of the variable :
guess10 = input("Letter: ")
Then, look at where it is used : only in this small part. Not after, not before.
So, instead of using different variables, you can **reuse"" the same (let's say guess
).
Then, you just need to repeat the same code 14 times (as in the original code).
So, you just need to wrap this part of code in a for a in range(14):
, replace guess10
by simply guess
, and that will reduce the line of codes used a lot.
Upvotes: 1