-1

I'm new in Python.

Hello Admin:

Make a list of five or more usernames, including the name 'admin'. Imagine you are writing code that will print a greeting to each user after they log in to a website. Loop through the list, and print a greeting to each user:

• If the username is 'admin', print a special greeting, such as Hello admin, would you like to see a status report?

• Otherwise, print a generic greeting, such as Hello Eric, thank you for logging in again.

Code:

u = input('Enter Username: ').title()

for user in usernames:
    if u in user == 'Admin':
        print('Welcom admin')
        if u in user:
            print('In list')
    else:
        print('Not in list')
4
  • 3
    What's the code that you've used so far? Commented Feb 13, 2020 at 16:02
  • u = input('Enter Username: ').title() for user in usernames: if u in user == 'Admin': print('Welcom admin') if u in user: print('In list') else: print('Not in list') Commented Feb 13, 2020 at 16:07
  • Edit your question to include any new informations (like your code) instead of burying it in the comment section. Dont forget to format it with markdown ;) Commented Feb 13, 2020 at 16:07
  • add that to your post/question Commented Feb 13, 2020 at 16:08

5 Answers 5

1

Make a list in the form users = [user1, user2, ...]

Then iterate the users list with a for loop and insert an if statement to contol if the user in the user list is the "admin" user to change the greeting.

Sign up to request clarification or add additional context in comments.

1 Comment

i have edited my question and post code on that i;m taking useriput how but loop is not breaking
1
username = input('Enter Username: ').title()

Once the input is given:

for username in usernames:
    if username in usernames:
       if username == 'Admin'
          print("Hello admin, would you like to see a status report?")
       else:
          print("Hello " + username + ", thank you for logging in again.")
    else:
        print("You are not on the user's list")

This will loop through all the values in the array and if the value is equal to admin it will print a special message, otherwise a generic message :)

3 Comments

if i run the it print all names in the list
actually i'm taking a user-input u = input('Enter Username: ').title()
Taking your example, you would first need to check if the user is in the list. Once the user is in the list then you can check if it is admin or not. I hope that helps :)
0

As per the question, you are looking for an answer

Create a list user_names(based on PEP 8 naming convention) and loop through them

user_names = ['eric', 'willie', 'admin', 'erin', 'ever']

for user in user_names:
    if user == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + user + ", thank you for logging in again!")

To see an answer as below

Hello eric, thank you for logging in again!
Hello willie, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello erin, thank you for logging in again!
Hello ever, thank you for logging in again!

If you want to just ask for a name as input and print the output based on the provided input here's the code

user = input('Enter Username: ').lower()

if user == 'admin':
    print(f'Hello {user}, would you like to see a status report?')
else:
    print(f'Hello {user.title()}, thank you for logging in again.')

With a predefined list where you will be checking the user in the user_name list, here's the answer

user = input('Enter Username: ').lower()

user_names = ["eric", "admin", "lever", "matt"]

if user in user_names:
    if user == 'admin':
        print(f'Hello {user}, would you like to see a status report?')
    else: 
        print(f'Hello {user.title()}, thank you for logging in again.')

else:
    print('Not in list')

Output:

enter image description here

9 Comments

thank-you very much you have defined in detail but i want to know that if we are checking user from predefined list then we need a loop or not
i have done this by if else but if you see the question it is telling looping through the list
Anywhere if you are using for...in or while, it's a loop. Using a conditional like if...else doesn't have any impact as you are checking whether the user is an admin or not. To go through a list, you need a loop
but the above for loop code would print all values and i want if user input admin and other usernames and they are in list then in list else not in list
Check the last code-snippet above, it won't print all the users
|
0

Your if statement is wrong. It should be:

for user in usernames:
    if user == 'admin':
        print(f'Hello {user}, would you like to see a status report?')
    else: 
        print(f'Hello {user}, thank you for logging in again.')

The above is without user input since what I understand that you are asked for is this. If you insist for user input:

u = input('Enter Username: ').title()

if u in usernames:
    if u == 'admin':
        print(f'Hello {u.lower()}, would you like to see a status report?')
    else: 
        print(f'Hello {u()}, thank you for logging in again.')

else:
    print('Not in list')

Also, for efficiency reasons you could consider converting the list into a set with usernames = set(usernames).

5 Comments

I have edited my answer switching .lower() with .title(). Could you try again?
'str' object is not callable
i have remove lower() this and now the code is running fine
can you answer me one more thing
what the difference between if u in user == 'Admin': and if u == 'admin':
-1
usernames=['fahad','riad','antano','Admin']

for username in usernames:
    if username.lower() == 'admin':
        print('Hello admin , would you like to see a status report')
    else:
        print(f"Hello {username.title()} , thank you for login again ")

1 Comment

Now please explain how is your answer different from other answers, I can't see a difference. Please only add answers if you have something new to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.