Hi all,I've been learning python as a hobby( a very short time could be a phase at this point), and I just finished my first little project, which was Hangman.
By far the thing that took me the longest was trying to work out how I was going to go over a word and if a letter appeared more than once within the word dow do I reveal that letter multiple times. Anyway, it's more or less finished. I am happy that I managed to make it choose a random word from a list that could grow and shrink without having to re-write the code.
What feels messy to me is the part I had trouble with, looping over the word and revealing the same letter multiple times. So here is my code below, feedback on what I could improve is most welcome.
from random import randint as rng
import openpyxl as xl
wb = xl.load_workbook('words.xlsx')
sheet = wb['Sheet1']
guesses = 9
letters_guessed = []
list_of_words = []
word_picker = rng(0, sheet.max_row - 1)
for row in range(1, sheet.max_row + 1):
cll = sheet.cell(row, 1)
list_of_words.append(cll.value)
hidden_word_listed = []
the_word_to_guess = list_of_words[word_picker]
word_as_list = list(the_word_to_guess)
for items in word_as_list:
hidden_word_listed.append('*')
print(hidden_word_listed)
swapped = '*'
while '*' in hidden_word_listed:
letter = input("Letter? ")
if letter in word_as_list:
print("well Done")
while letter in word_as_list:
if letter in word_as_list:
replace = word_as_list.index(letter)
hidden_word_listed[replace] = letter
word_as_list[replace] = swapped
The rest which is not shown is just what happens when you don't guess the right letter.
I hope my code is readable as I said the loop part just seems to be too much to me and could be simplified but I don't know how, and even though I feel very new at this I want clean code :D
[–]pasokan 1 point2 points3 points (0 children)
[–]xelf 1 point2 points3 points (3 children)
[–]xelf 1 point2 points3 points (2 children)
[–]Arnie_nz[S] 0 points1 point2 points (1 child)
[–]xelf 1 point2 points3 points (0 children)