简体   繁体   中英

Welcome messages for different userNames

I want a python code to print greeting messages for users when logged in: if the userName is admin then it should print "Hello admin you're welcome, would you want to see a status update?", and if the userName is other than admin then a different welcome message is printed:

I tried the code below but am not getting it right:

userNames = ["jack2", "admin", "lucy21", "angeUt", "lacky53"]

userName = "admin"
if userName == "admin":
    # ** print this below if admin is the userName **
    print("Hello " + userName + " you are welcome, would you like to see a status update?")
else:
    print("other message")
    # ** this should be printed when the userName is for example jack2. **

Please note I just started python barely a week now. I just didn't get the code right because am just starting out with programming.

在此处输入图像描述

The first prompt says to loop through the list of user names, and print a special message if the name is "admin":

def main():
    userNames = ["jack2", "admin", "lucy21", "angeUt", "lacky53"]
    for user in userNames:
        if user == "admin":
            print("Hello admin would you like to see a status report")
        else:
            print("Hello {}".format(user))

if __name__ == '__main__':
    main()

You can easily check if the username is in the list of names with user in userNames . Then you would just have to check if it's the admin, for the more specific message.

userNames = ["jack2", "admin", "lucy21", "angeUt", "lacky53"]

user = "admin"

if user in userNames:
    if user == "admin":
        print ("Hello " + user + " you are welcome, would you like to see a status update?")
    else:
        print ("Hello " + user + " thanks for logging in again.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM