Reputation: 17
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')
Upvotes: -1
Views: 1042
Reputation: 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 ")
Upvotes: -1
Reputation: 2865
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:
Upvotes: 0
Reputation: 11
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 :)
Upvotes: 1
Reputation: 2094
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)
.
Upvotes: 0
Reputation: 146
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.
Upvotes: 1