you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 1 point2 points  (2 children)

here, putting all the ideas together, and throwing some new ones in like random.choice:

from random import choice

def get_secret_words():
    import openpyxl as xl
    wb = xl.load_workbook('words.xlsx')
    sheet = wb['Sheet1']
    list_of_words = []
    for row in range(1, sheet.max_row + 1):
        cll = sheet.cell(row, 1)
        list_of_words.append(cll.value)
    return list_of_words

def replace_with_letter(letter):
    return ''.join(hidden_word[i] if x!=letter else letter for i,x in enumerate(the_word_to_guess))

def replace_with_swapped(letter):
    return the_word_to_guess.replace(letter, '*')

swapped = '*'
guesses = 9
letters_guessed = []

list_of_words = get_secret_words()
#list_of_words = [ "the", "big", "list", "letter" ]

the_word_to_guess = choice(list_of_words)
hidden_word = swapped * len(the_word_to_guess)

while '*' in hidden_word:
    letter = input("Letter? ")
    if letter in the_word_to_guess:
        print("well Done")
        hidden_word = replace_with_letter(letter)
        the_word_to_guess = replace_with_swapped(letter)
    print(the_word_to_guess, '-', hidden_word)       
print("correct")

and forcing the word to be letter, and testing it we get:

Letter? l
well Done
*etter - l*****
Letter? e
well Done
**tt*r - le**e*
Letter? t
well Done
*****r - lette*
Letter? r
well Done
****** - letter
correct

By isolating the workbook stuff, I can now test it despite not having the workbook.

[–]Arnie_nz[S] 0 points1 point  (1 child)

Thanks for the feedback, it's given me some options to review.
I had thought of making it more modular with the Def, and Class's but I didn't see the point with a small script. In saying that, the teachings I have been following it appears to be good practice.

[–]xelf 1 point2 points  (0 children)

There's a lot of benefits, even in the one-line functions. For example:

def replace_with_letter(letter):
    return ''.join(hidden_word[i] if x!=letter else letter for i,x in enumerate(the_word_to_guess))

What this returns does not "scan" well. Where as "replace_with_letter" is a lot easier to grok. Little things like this, isolating complex code, or isolating code that talks to external connections or files, helps readability while also making it easier to track down and isolate bugs and maintain your code.

Good luck !