[Python] "print" after "msvcrt.getch" doesn't work

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    So, I am using python and I wrote a code to scan a password. So, as password should not be displayed openly, I scan one character using msvcrt.getch() and then do print('*', end='').
    Here's the code:

    if __name__ == '__main__':
    string = ''
    ch = msvcrt.getch()
    while ch != b'\r':
    print('*', end='')
    string += ch.decode('utf-8')
    ch = msvcrt.getch()
    print('\nEntered string is: ', string)


    The problem here is, the *'s are getting printed after I type the whole "password". But, what I was expecting is a * getting printed every time I enter a character. I am running the code in terminal.

    I think I structured the code properly so why doesn't it work?
    (Also, if there are any others ways of scanning passwords, please do mention them)

    Continue reading...

Compartilhe esta Página