Why am I being asked to re-enter the username?

I have just started to learn programming and I’m trying to create a simple username module where the user is asked to enter a username of their choice. And, I want to use the username they’ve entered in the text messages I’m displaying on the screen. I’m sorry if my question is not clear. Please, do let it me know if I need to re-iterate my query.

So, this is what I want to accomplish:

  • (screen_1) Ask user to enter username:
  • (screen_2) Display username with a welcome message:
  • (screen_3) include entered username with whatever other modules I want to incorporate without having ask the user to enter their username again

And, this is what is happening:

  • (screen_1) Being asked to enter a username:
  • (screen_2) Username is being displayed with a welcome message:
  • (screen_1) Being asked to enter username again:
  • (screen_3) username being displayed with the message:

I don’t want to have to enter the username again. Why is it happening and how do I fix that, please?

My modules:

index.py

# Ask user to enter their username and software greets them
import username
print("Welcome " + username.usern() + ",""\n")
input("Press Enter to continue...")

# execute the Main Menu
import mainmenu
mainmenu.mMenu()

username.py

# Ask user to enter their username and display username
def usern():
   return str(input('Enter your Username: '))

mainmenu.py

def mMenu():
    # Clear screen
    import os
    os.system('cls')
#  display the username
    import username
    print(username.usern() + ",")
    # List of options
    print("Random Message\n")
    input("Press Enter to continue...")

It is great that your question is well structured! You did not write how you execute the program. I am guessing:

$ python3 index.py

Your function username.usern() asks for the username and you are calling it twice:

  1. file index.py:
    print("Welcome " + username.usern() + ",""\n")
  2. file mainmenu.py:
    print(username.usern() + ",") — inside mainmenu.mMenu() called from index.py

You probably want to ask for the username only inside mainmenu.mMenu() or alternatively inside index.py but instead of displaying it right there, give it as a parameter to the mainmenu.mMenu() function which can then display it.

Just some basic notes about style of writing Python code:

  • Use more understandable identifier names and follow Python style - probably:
    • usern()ask_user_name()
    • mMenu()main_menu()
    • index.py should rather be named as you want the whole program to be called
  • Use docstrings to describe functions and take care it describes the real behaviour:
def ask_user_name():
    """Ask user to enter their username and return it."""
    ...
  • Import at the beginning of the files. (Importing later is only for advanced very special cases.)
  • Normally all the commands except definitions and includes should be inside functions. So also in index.py enclose the commands inside def main() and call main() at the end of the file.
  • Be aware of using platform-specific functionality: os.system('cls') probably works only on Windows?
  • Do not let comments break the visual indentation of the code like your # display the username inside mMenu().

Edit:

  • Good style is not to switch between quotes ' and " without a reason. Stick to one of them.
    (Python uses ' in its messages. The popular totalitarian :smiley: formatter black enforces ".)
  • It is normally preferred to use f-strings:
    print("Welcome " + username.usern() + ",""\n")
    print(f"Welcome {username.usern()},\n")
1 Like

By Václav Brožík via Discussions on Python.org at 09Jun2022 07:54:

  • Do not let comments break the indentation of the code like your # display the username inside mMenu().

I’m pretty sure comments do not affect the logical indentation. I
routinely comment out code with “##” at the start of the line, and
remaining code is unaffected.

Though in other crcumstances I like my comment indentation to match the
code; if nothing else it stops the comments breaking my internal idea
about the indentation.

Cheers,
Cameron Simpson cs@cskk.id.au

@cameron
That is exactly what I meant. I just did not word it clearly. Thanks :slight_smile:
I added word “visual”. I hope it is better now :slight_smile:

Welcome, Criss.

Your program asks for the username multiple times because you aren’t storing its value anywhere.

Every time username.usern() appears in your code, it runs the usern() function and asks for a username.

Try naming your top-level module ‘main.py’ and store the username into a variable there. That will make userName global so that any other module can see the userName variable and use its value.

P.S. The username.py module is a bit of overkill. Functions are useful if you plan to invoke them more than once, whereas you’ve said you only want to enter the username once. I realize that you’re doing this as an exercise, but the degree of modularity is complicating your code structure. If it suits your immediate learning goals, keep it.

P.P.S. input() returns a string value, so you don’t need to convert it with str(input()). It’s good that you’ve clued into the conversion, though, because MANY new programmers get caught by surprise when asking the user to enter numbers which are then used for math or numerical sorting. In that case int(input()) is the way to go.