r/learnpython • • Mar 23 '24

Need to hide user input but in a loop

Now I found out that i can use "getpass()" to hide my user input. But I put it in a loop and it doesn't like it... literally no reason to not be able to do it.

while True:
    word = getpass.getpass(prompt = ">Executioner, what will be the word? Make sure the guesser does not see.", stream = None).lower()
    for char in word:
        if char.isdigit():
            print(">Your word cannot have a number. Try again.")
            valid_letter = False
            break
        else:
            valid_letter = True
    if valid_letter == True and char == word[-1]:
        break

also how do i set the language in the code block feature i want them pretty colooors :)

EDIT: ERROR HERE

GetPassWarning: Can not control echo on the terminal.

EDIT EDIT: so it does work but theres still an issue. the prompt appears in the beginning of the program with the error but i want it to appear after the game description.

0 Upvotes

31 comments sorted by

3

u/socal_nerdtastic Mar 23 '24

I don't see anything wrong with this code. What's it doing / not doing that you want it to not do / do?

-7

u/creamHASbeentaKeN Mar 23 '24

NOOO I WENT BACK TO GET THE ERROR BUT I HAVENT COPIED IT AFTER I COPIED THAT PIECE OF CODE AND ITS GONE

-9

u/creamHASbeentaKeN Mar 23 '24

CAN I GET IT IN MY CLIPBOARD???

-8

u/creamHASbeentaKeN Mar 23 '24

i cant.... :(((((

2

u/Buttleston Mar 23 '24

There's no code coloring on reddit

"Doesn't like it" is not really a description of a problem" - copy/paste the actual error message and stack trace if you got an exception. If not, explain what you wanted to happen, and what happened instead

1

u/creamHASbeentaKeN Mar 23 '24

my bad but i cant get the error the website reloaded and my stuff is gone ;( but i copied a save earlier but idk if i can access it in my clipboard since i copied that piece of code now!

3

u/Buttleston Mar 23 '24

You literally posted your code here. What happens when you run that?

1

u/creamHASbeentaKeN Mar 23 '24

no that was just part of my code i posted

but the website reloaded before i could retrieve the whole thing!
the reason executioner input was in a while loop was because i had to check if it had numbers in it then ask it again if it was invalid
but i also wanted the input to not be visible to the guesser and had to use getpass but i got the "getpass password might echo" or some chaite

8

u/Buttleston Mar 23 '24

It doesn't make any sense to post some code that doesn't have any problems with it and ask why it doesn't work. You see that right?

The code you posted works fine for me, as long as I import getpass. If it doesn't work for you in isolation, great, post the error you get. If it does, then this code isn't your problem, and something else is

When you're trying to get help from people, you have to give them information that could let them help you.

Honestly, a short, well-formatted OP like yours is great, I wish all of them were like that. But if this isn't the code with a problem, then there's nothing anyone can do for you.

1

u/creamHASbeentaKeN Mar 23 '24 edited Mar 26 '24

i think im being unclear here sorry

i did post the part with the problem. i didnt post the rest

heres the error

/usr/lib/python3.8/getpass.py:91: GetPassWarning: Can not control echo on the terminal.
    passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
>Executioner, what will be the word?

edit: my dumahh thought i needed the whole code to get the error my brain was unclear

1

u/lfdfq Mar 24 '24

That's not an error, it's a warning. The program continued to run after it just fine.

Normally when you call getpass() it hides the password. When you type into a prompt you can normally see the characters you are typing, but getpass() tries to turn this off so someone watching you type can't see what the password is.

That message is just warning you that, because of the magic website you're running it in, that getpass can't hide the password, so when you type it in you should know it might be visible (e.g. if you're streaming/recording/there's someone stood there).

1

u/Buttleston Mar 23 '24

It works for me - if I import getpass. If you are using a function from a module, you have to import it first.

1

u/creamHASbeentaKeN Mar 23 '24

i did but the error said "it might echo"

4

u/Buttleston Mar 23 '24

Run the code, paste the actual error. I know you're new to this, but people can not help you with vague comments like this. The error and stack trace might look like nonsense to you, but it's how to diagnose problems

3

u/stebrepar Mar 23 '24

the error said "it might echo"

If you're running the code via IDLE (like pressing F5 while editing the code file), yes, you'll get a message warning you that the password you enter will print as you type it in. But if you run it from the command line, there isn't a warning, and it doesn't echo as you type.

1

u/creamHASbeentaKeN Mar 23 '24 edited Mar 23 '24

im running it via online-python.com. temporary device so im not downloading vscode. + it my mom's but thats my problem

1

u/Buttleston Mar 23 '24

Also use

if valid_letter

instead of

if valid_letter == True

And I don't think this part serves any purpose

char == word[-1]

1

u/creamHASbeentaKeN Mar 23 '24

its to check that its finished checking the word

1

u/Buttleston Mar 23 '24

The only circumstance where that wouldn't happen would be if valid_letter was False, so it doesn't improve the if statement at all

0

u/creamHASbeentaKeN Mar 23 '24

i meant char == word[-1]

3

u/Buttleston Mar 23 '24

I know. The only way "char" won't be the last letter of the word is if you abort the loop early. The only way that happens is if one of the chars was a number and you aborted the loop early. Therefore, that char == word[-1] does not add any additional information to the if statement

if valid_letter is false -> doesn't matter what "char" is
if valid_letter is true -> you already know it examined the whole word

1

u/creamHASbeentaKeN Mar 23 '24 edited Mar 24 '24

took me dumahh a while to take in this info

valid_letter == true situation never breaks and it turns back to false if number is detected which then breaks the for loop

the only way valid_letter is true in the end is if it was never false

i see now thank you

1

u/Im_Easy Mar 23 '24
if valid_letter == True and char == word[-1]: 
    break

Not sure if this is just Reddit formatting on mobile, but this doesn't look like it's in your For Loop. If this is intentional, it's weird to reference a loop variable outside it's loop. If it is in the loop, then ignore me.

1

u/creamHASbeentaKeN Mar 23 '24

ye it wasnt in the for loop i thought when valid_letter == True it would continue after the for loop

1

u/socal_nerdtastic Mar 23 '24

also how do i set the language in the code block feature i want them pretty colooors :)

Sorry, reddit does not have syntax highlighting

-1

u/creamHASbeentaKeN Mar 23 '24

yall i lost the full code because crappy laptop wifi
ima work on it tomorrow but the problem still remains
why do i always have to start over completely at least once when i code ;(

4

u/Im_Easy Mar 23 '24

What code editor are you using and why doesn't it have auto-save?

1

u/creamHASbeentaKeN Mar 23 '24 edited Mar 23 '24

online-python.com.. temporary device so im not planning to download vscode plus its my mom's

but thats my problem

4

u/NYX_T_RYX Mar 23 '24

why do i always have to start over completely at least once when i code ;(

I'll go with "layer 8 problem".

Stop coding online and get an ide.

0

u/creamHASbeentaKeN Mar 23 '24

this laptop aint mine i wont download ide
thats my problem tho