#πŸ”’ Hang man game: While loop problem in python (please help!)

100 messages Β· Page 1 of 1 (latest)

drifting iron
#

I'm pretty new to pythn and still learning. So basically, I was trying to make hang man but like what I want is for the user to guess a letter and it'll show and keep it then it'll ask again the same question (Guess a letter) then either if it is wrong/right it'll ask until all the lives are lost or you when without losing 6 lives. But I can't seem to get the while loop to do this for me.

bitter egretBOT
#

@drifting iron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

maiden bough
#

!paste

bitter egretBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

drifting iron
#

do you mind if i can talk in dm's?

#

this issue is kind of annoying 😭

lone depot
#

Better to talk here.

drifting iron
#

Alright πŸ‘

maiden bough
bitter egretBOT
#

Hey @drifting iron!

Please edit your message to use a code block

You are using the wrong character instead of backticks. Use ```, not '''.

maiden bough
#

put it in a pastebin as linked

drifting iron
#

Ohhh

#

there we go

maiden bough
#

now delete your other msg

drifting iron
#

alright

lone depot
#

Right. So you've having trouble writing a while loop for the guess input?

maiden bough
#

oh btw you can do

placeholder = "_" * len(chosen_word)
drifting iron
#

I'm kind of not sure but my code so far works. I just want it to ask me to put another input in until I either loose all my lives or win the game and I also want it to keep the letter I am currently guessing and not entirly change it and delete all the stuff I made progress on earlier

drifting iron
#

or exponent*

lone depot
#

It multiplies the "_"

maiden bough
#

multiplies...

#

exponent in Python is **

drifting iron
#

ohh my bad apostrophy*

maiden bough
#

?

lone depot
#

Thopugh the code as written fills in the guessed letters, so the full length "_"* doesn't really work.

#

Anyway, it sounds like you want to wrap the entire bottom of the script from the guess = line downward in a while loop, to keep guessing?

drifting iron
maiden bough
#

can also call it a star

drifting iron
#

then afterwards I'm planning to use if functions to end the loop when I lose all my lives or when all "_" is filled up with charaters

lone depot
#

You can, with a little work, put all that in the while condition.

#

A while loop runs while its condition is true.

#

So make the condition express "when can the user guess again?"

#

Sounds like:

  • lives not used up
  • word not fully guessed
#

Can you write those 2 conditions?

drifting iron
#

I'm not sure how too... can you by chance give me some hints?

lone depot
#

Do you know how many lives the user has?

drifting iron
#

yes 6 and I expressed it count -= 1 so everytime I get it wrong it deducts from count = 6

lone depot
#

So they start with 6. But count expresses how many are left.

#

So what would you write to test if the lives are not all gone? Or that they are all gone?

drifting iron
#

a if else statement?

lone depot
#

An if-statement can be used to test a condition. What about the condition itself? If you're happier writing an if-statement, do that.

drifting iron
#

oh so theres a better function than if statements?

maiden bough
drifting iron
#

right now I only learned about loops and if else statments i'm kinda new

lone depot
#

It's more what you intend to do. The important thing here is to decide when things should happen. So for a programme, that means writing the condition for something.

delicate echo
#

a while statement is better

#

while the game is not over, ask user to guess

lone depot
#

If the number of lives left is in count, what would you say in English about when the user may make another guess?

lone depot
delicate echo
#

kk

drifting iron
lone depot
#

Ok. Would that be count > 0, as a statement of what must be true?

drifting iron
#

yes

lone depot
#

Ok. So a while loop might start:

while count > 0:
    guess = ......
    ... the rest, indented under the while ...
drifting iron
#

yeah

#

so uhm now what...

lone depot
#

Well do that first maybe.

drifting iron
#

okok

#

while count > 0:
guess = input("Guess a letter: ").lower()
for letter in chosen_word:

    if letter == guess:
        display += letter
        correct = True
    else:
        display += "_"
bitter egretBOT
#

Hey @drifting iron!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
maiden bough
#

the importance of using a code block is to avoid formatting in markdown

#

(as well as make code easier to read by making it in a monospaced font)

drifting iron
#

is there a command for that?

lone depot
drifting iron
#
while count > 0:
    guess = input("Guess a letter: ").lower()
    for letter in chosenword:

        if letter == guess:
            display += letter
            correct = True
        else:
            display += ""
maiden bough
#

tada

lone depot
#

Yes, like that.

#

Seems incomplete.

maiden bough
#

I know what's wrong

#

you copy-pasted from in discord

#

instead of just repasting

#

selecting and copying only takes the visible characters, not the ones that have been used for formatting

#

so you are missing two underscores from the code

#

to get the invisible characters, right click and do "copy text"

drifting iron
#

okok

maiden bough
#

I would suggest running your code now and seeing if you notice issues

#

or get errors

drifting iron
#
________
Guess a letter: a
Guess a letter: a
Guess a letter: a
Guess a letter: a
Guess a letter: 
#

this is what happens

maiden bough
#

ok, so what is not happening that should?

drifting iron
#

i'm suppoe to guess then it'll show if i'm right or not then replace underscores that contain the matching guessing number (Ex. a____)

maiden bough
#

so we are missing output. Now, add a print to do just what you described

neat widget
maiden bough
#

it's a game in the terminal, who cares if the words are selected not totally randomly

neat widget
#

lol

drifting iron
#
Guess a letter: a
Guess a letter: a
_a____
Guess a letter: a
_a_____a____
Guess a letter: 
#

now this is te output

#

I printed the variable display inside the while loop

maiden bough
drifting iron
#

nvm I got it to work!

#
count = 6
guessed_letters = []
while count > 0:
    guess = input("Guess a letter: ").lower()
    if guess not in guessed_letters:
        guessed_letters.append(guess)
    display = ""
    correct = False
    for letter in chosen_word:
        if letter in guessed_letters:
            display += letter
            if letter == guess:
               correct = True
        else:
            display += "_"
    print(display)
    if not correct:
        count -= 1
        print(stages[count])
#

that was lowkey tough to figure out

bitter egretBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.