input() inserting new lines when pressing enter

I've ran into an odd issue where I have some input() calls where the 2nd input call, when typing input in the console and pressing Enter, will add a newline instead of sending the input back to my program. I've come up with the smallest reproducible example I could come up, below.

Has this happened to others, and if so, is there a fix for it? I've ran the code with IDLE and Python executable, in both cases the code works correctly, I'm only having this issue in PyCharm. Here is a screenshot demonstrating the issue. It exited with -1 because I had to manually shut down the running program.

Here is my code:

import getpass


def main_menu() -> None:
print('Menu:\n1. Create new user\n2. Log in')
valid_selections = [1, 2]
input_is_valid = False
selection = None
while not input_is_valid:
try:
selection = int(input('Selection: '))
if selection in valid_selections:
input_is_valid = True
else:
print('The number you entered is not a valid selection.')
except ValueError:
print('The value you entered is not a number.')
handle_main_menu_selection(selection)


def handle_main_menu_selection(selection: int) -> None:
if selection == 1:
create_new_user_menu()
elif selection == 2:
user_login_menu()
else:
raise ValueError(f'Selection {selection} is invalid.')


def create_new_user_menu() -> None:
menu = '\n'.join([
'---',
'Account creation'
])
print(menu)
user_added_successfully = False
username = ''
while not user_added_successfully:
try:
username = get_username_input()
password = get_password_input()
user_added_successfully = try_adding_user(username, password)
if not user_added_successfully:
print(f'Username "{username}" already exists.')
except ValueError as error:
print(str(error))
# for testing purposes only
new_user = user_storage.retrieve_user(username)
print(new_user)
time.sleep(10)


def user_login_menu() -> None:
menu = '\n'.join([
'---',
'User login',
'---'
])
print(menu)
login_successful = False
while not login_successful:
username = get_username_input()
password = get_password_input()
print(username)


def get_username_input() -> str:
username = input('Enter username: ')
return username


def get_password_input() -> str:
password = getpass.getpass('Enter password: ')
return password


if __name__ == '__main__':
main_menu()

Please sign in to leave a comment.