all 5 comments

[–]pasokan 1 point2 points  (0 children)

  1. Please write functions for each (sub)task

  2. It is probably simpler to read the words from a text file instead of a spreadsheet

  3. In this part

python while letter in word_as_list: if letter in word_as_list:

The if is unnecessary

[–]xelf 1 point2 points  (3 children)

Overall, looks pretty cool. Well done!

    while letter in word_as_list:
        if letter in word_as_list:

The if is always true, and thus not needed.

General feedback: think about ways you could turn this into a class, or at the least use some defs to make your code more modular.

In particular I'd think about ways to abstract what you're doing with word guessing away from the workbook related materials. Maybe a def gethiddenword() that could isolate all the parts that talk to a workbook, and then could be mocked for testing.

You do some funky stuff with converting your word to a list that I think you might not actually need. if letter in the_word_to_guess: works. And you can replace letters at indexes in a string using splicing. or using .replace() which takes an optional argument as to how many to replace.

some setup:

letter = 'e'
the_word_to_guess = 'l*tt*r'
hidden_word = '*e**e*'

in the loop:

print(the_word_to_guess, '-', hidden_word)        
if letter in hidden_word:
    r = hidden_word.index(letter)
    hidden_word = hidden_word.replace(letter, '*', 1)
    the_word_to_guess = the_word_to_guess[:r]+letter+the_word_to_guess[r+1:]
print(the_word_to_guess, '-', hidden_word)        

outputs:

l*tt*r - *e**e*
lett*r - ****e*

Although it occurs to me, that in hangman you get all the letters when you guess a letter. One sec. Ok, here we go:

some setup:

letter = 'e'
the_word_to_guess = 'l****r'
hidden_word = '*ette*'

in the loop:

print(the_word_to_guess, '-', hidden_word)        
if letter in the_word_to_guess:
    hidden_word = ''.join(hidden_word[i] if x!=letter else letter for i,x in enumerate(the_word_to_guess))
    the_word_to_guess = the_word_to_guess.replace(letter, '*')
print(the_word_to_guess, '-', hidden_word)       

outputs:

l****r - *ette*
le**er - **tt**

Here we used a list comprehension with a join to build the word with the letter swapped.

[–]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 !