0
How do I print this once and kill many with one stone in Python?
Sorry, the code doesn't run well on SL but I posted it to show you what I mean: I want to print the dashes "". join(state) just once but get an output where the user sees the whole word dashed up, and then sees the letters filled in with each correct guess, and yet I don't want it to display during iterations with wrong guesses. I assigned "". join(state) into the guess variable and moved that variable around within the while loop, but I couldn't get to have it all. say my word is "okay" ---- then user takes a guess and inputs k -k-- then a wrong guess, inputting r, no display Is there a way of doing this by using "". join(state) just once? https://code.sololearn.com/cjLMA2d49yf2/?ref=app
6 Réponses
+ 1
Assuming you run it on your Window system: you can read the input with getpass() at line 22 no prompt or replace \n with \r. If you go with no prompt, insert it before 21. Use this code to update your word instead of line 28:
print('\rEnter a letter: {guess} ', end='', flush=True)
Use this code to display a strike instead of line 37:
print('\rEnter a letter: {guess} Strike ', mis, end='', flush=True)
+ 1
What it does is \r goes back to the beginning of the line and redisplays it over itself. However, it depends on the system as to it working correctly. Some systems treat \r as \n so next line. Others ignore it so same line continuation.
+ 1
John Wells I am going to try hard to understand these. I cannot make sure if you'd sent it before I made the changes to it in accordance with the reply I got from you in the other question. I guess not? But then you would've seen the prompt. There's some stuff in your suggestion that I've never seen or implemented before.
Nice material to study. And that I sure will, after I get some sleep.
Sir, thank you so much.
+ 1
If you hide the input, you can redisplay the line with the prompt and current guess.
The word is ----, what letter next:
This would get replaced with the following line after reading t.
The word is T--T, what letter next:
This would get replaced with the following after reading h.
The word is T--T, strike one, what letter next:
Ecetera... The moment you let the user's input display occur you are no longer dealing with a single line and must use a graphic library to know where to go.
+ 1
If you stay on a single line, '\r' is the character to put you to the beginning. What you print can not use '\n' as that moves to the next line (print's default end string) so you must use end='' to prevent it. This does not work every where.
0
John Wells Because I'd already altered the code before I saw your replies here, I was unable to make sure which lines you were talking about, although I think I can guess.(and I've made further changes, small ones, since then since it was already changed)
I don't want to hide the letter input butrather the word input high above, assigned to k.
So that was what I did.
As for replacing print(guess) with what you said to put for what formerly was line 28, and I am guessing it's print(guess), it outputs Enter a letter: {guess}, with "guess" unchanged.
So if I get it right, I could hide second user's input the way you said, to then flush it out with either the updated guess string if the letter is in the word or with Strike feedback as wrong, each in one line.
I haven't hidden the letter input but I guess the second change should have still worked.
I can't find out where I'm implementing it wrong. I simply copied the code (on win python)