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.
#π Hang man game: While loop problem in python (please help!)
100 messages Β· Page 1 of 1 (latest)
@drifting iron
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.
Closes after a period of inactivity, or when you send !close.
!paste
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.
Better to talk here.
Alright π
the main goal of this server is to help
Hey @drifting iron!
Please edit your message to use a code block
You are using the wrong character instead of backticks. Use ```, not '''.
put it in a pastebin as linked
now delete your other msg
alright
Right. So you've having trouble writing a while loop for the guess input?
oh btw you can do
placeholder = "_" * len(chosen_word)
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
oh what does the multiplication sign do?
or exponent*
It multiplies the "_"
ohh my bad apostrophy*
?
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?
aterick π*
yes yes
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
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?
I'm not sure how too... can you by chance give me some hints?
Do you know how many lives the user has?
yes 6 and I expressed it count -= 1 so everytime I get it wrong it deducts from count = 6
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?
a if else statement?
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.
oh so theres a better function than if statements?
if-statement. You will learn what a "function" is later
right now I only learned about loops and if else statments i'm kinda new
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.
If the number of lives left is in count, what would you say in English about when the user may make another guess?
We're getting to that.
kk
if count is still above 0 then the user may make another guess?
Ok. Would that be count > 0, as a statement of what must be true?
yes
Ok. So a while loop might start:
while count > 0:
guess = ......
... the rest, indented under the while ...
Well do that first maybe.
okok
while count > 0:
guess = input("Guess a letter: ").lower()
for letter in chosen_word:
if letter == guess:
display += letter
correct = True
else:
display += "_"
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!')```
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)
is there a command for that?
Put a ```py line above the code and a ``` line afterwards.
while count > 0:
guess = input("Guess a letter: ").lower()
for letter in chosenword:
if letter == guess:
display += letter
correct = True
else:
display += ""
tada
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"
okok
I would suggest running your code now and seeing if you notice issues
or get errors
________
Guess a letter: a
Guess a letter: a
Guess a letter: a
Guess a letter: a
Guess a letter:
this is what happens
ok, so what is not happening that should?
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____)
so we are missing output. Now, add a print to do just what you described
This is off topic and It doesnβt fix it, but I think use secrets module over random.
there is no reason whatsoever to do that
it's a game in the terminal, who cares if the words are selected not totally randomly
lol
oh alright alright lemme do that rq
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
what issues do you see with it?
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
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.