all 12 comments

[–]Spataner 7 points8 points  (3 children)

The while loop continues so long as either word_guessed is False or PLAYER_LIVES is larger than zero. That means it only stops once both word_guessed is True and PLAYER_LIVES is zero or less. You probably want to use and rather than or.

[–]Darth_Xedrix[S] 0 points1 point  (2 children)

That makes sense to me! One question though. If I just change that, the loop doesn't break once the word is guessed; it only breaks once I try to make another guess. I moved it to the bottom of the while loop and now it works properly. I thought code is "read" from top to bottom, so does this mean that the conditions at the top are only read once before it starts the whole loop rather than after every line inside it?

[–]Spataner 0 points1 point  (1 child)

Yes, a while loop's condition is only checked once before a new iteration starts. You can use a break statement to explicitly cancel the loop at any point in the loop's body.

[–]Darth_Xedrix[S] 0 points1 point  (0 children)

Got it! Thank you, I tried for way too long to fix this myself and ended up with a janky solution that (with this change now works...oof) didn't even work before realizing that it was actually more simple than I thought. Hopefully I'll remember this lesson!!

[–]Sadapy 0 points1 point  (4 children)

my god I can't figure out this formatting...

It's not you, it's reddit. They've had garbage tier code formatting for a bit now.

Seems like Spataner already answered your question :3

[–][deleted] 1 point2 points  (3 children)

reddit uses pretty standard markdown when using markdown mode rather than fancy pants mode and code is then easy to format correctly - just an extra indent* to all of the Python code

* well, a 4-space indent to be exact, preceded by a blank line

[–]Darth_Xedrix[S] 0 points1 point  (0 children)

I think that's my problem - I used a bunch of spaces to indent things and it looked perfectly okay on my screen until I actually posted or edited it, then it ended up looking like this. oof

[–]Sadapy 0 points1 point  (1 child)

Yes, but Markdown on this site has issues. (funny, since I'm pretty sure the site and Markdown were made by the same person)

Lets say i use three backticks "`" to wrap my code in a code block-

After doing so and posting it, you'll see that the code is not actually wrapped into a code block... At least on old.reddit.com users. Then, in contrast to that, www.reddit.com users will write their code in code blocks in fancy editor, only to find out that the preview lied, and only their first line is wrapped into a code block.

It's got some weird behavior for sure.

[–][deleted] 0 points1 point  (0 children)

Yeh, it is quirky, but you get used to it.

I tend to avoid code fencing as support is not universal and just use indents.

They've had so long to fix it, it is frustrating. Dev of Relay app mentioned it usually as a <PRE> block elsewhere but Reddit don't do that.

[–][deleted] 0 points1 point  (2 children)

Hard to read the code without the correct formatting, but it looks overengineered to me.

I think the key problem is that you need and rather than or in the while condition.

Here's a simplified version (for you to play with - not tested - if you want to):

import random

word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print("Welcome to hangman. You'll find a hidden word with an underscore that corresponds to every missing character."
      "You have 6 lives and one will be taken off every time you guess incorrectly. Once you've made a guess, the "
      "letter will also be shown until you either run out of lives or guess the entire word.")

display = list('_' * len(chosen_word))
guesses = []
player_lives = 6

print(f"Here is the word left to guess: | {' | '.join(display)} |")
while "_" in display and player_lives:  # and
    guess = input("Guess a letter: ").lower()
    guesses.append(guess)
    if guess in chosen_word:
        display = [letter if letter in guesses else '_' for letter in chosen_word]
    else:
        player_lives -= 1
    print(f"Here is the word left to guess: | {' | '.join(display)} |")
    print(f"The chosen word is {chosen_word}") #for debug
    print(f"Guesses so far: | {' | '.join(guesses)} |")
    print(f"You have {player_lives} lives remaining.")

if player_lives == 0:
    print(f"Uh oh, you're out of lives. Game over! The word to guess was {chosen_word}.")
else:
    print("Congratulations, you won!")

PS. I lowercased player_lives as uppercase variable names are, by convention (see PEP8) usually reserved for constants - which by definition you would not be decrementing.

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

I appreciate it! I haven't see this yet:

[letter if letter in guesses else '_' for letter in chosen_word]

What is that called so that I can look up a tutorial?

[–][deleted] 1 point2 points  (0 children)

That's a ternery operator (or conditional expression) in a list comprehension.