π Python Crash Course – Chapter 5: if Statements – Exercise 5-10 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 5-10
Task:
Do the following to create a program that simulates
how websites ensure that everyone has a unique username .
• Make a list of five or more usernames called current_users .
• Make another list of five usernames called new_users . Make sure one or
two of the new usernames are also in the current_users list .
• Loop through the new_users list to see if each new username has already
been used . If it has, print a message that the person will need to enter a
new username . If a username has not been used, print a message saying
that the username is available .
• Make sure your comparison is case insensitive . If 'John' has been used,
'JOHN' should not be accepted
✅ Solution Code:
current_users = ["tony","natasha","hulk","spider","panther"]
new_users = ["TonY","Hwakeye"]
for new_user in new_users:
if new_user.lower() in current_users:
print(f'Username {new_user} cannot be accepted')
else:
print(f'username : {new_user} accepted')
π§ Code Explanation:
This Python code simulates how websites ensure that usernames are unique by comparing new usernames with existing ones. Here's how it works:
-
Create a list of current users:
The variable
current_users
is assigned a list of existing usernames:["tony", "natasha", "hulk", "spider", "panther"]
. These represent usernames that are already taken. -
Create a list of new users:
The variable
new_users
is assigned a list of usernames that are being requested:["TonY", "Hwakeye"]
. Some of these usernames may already exist in thecurrent_users
list. -
Loop through new usernames:
A
for
loop iterates through each username in thenew_users
list. The variablenew_user
takes on each value in the list, one at a time. -
Check for case-insensitive matches:
The
lower()
method is used to convert both thenew_user
and the usernames incurrent_users
to lowercase. This ensures that the comparison is case-insensitive (e.g., "TonY" matches "tony"). -
Determine username availability:
If the lowercase version of
new_user
is found incurrent_users
, a message is displayed indicating that the username is already taken. Otherwise, a message is displayed indicating that the username is available.
This code demonstrates how to handle case-insensitive comparisons and ensure unique usernames using lists and loops in Python.
π Output:
Username TonY cannot be accepted
username : Hwakeye accepted
π Related Exercises from Chapter 5:
- ➤ Exercise 5-1 | Conditional Tests
- ➤ Exercise 5-2 | More Conditional Tests
- ➤ Exercise 5-3 | Alien Colors #1
- ➤ Exercise 5-4 | Alien Colors #2
- ➤ Exercise 5-5 | Alien Colors #3
- ➤ Exercise 5-6 | Stages of Life
- ➤ Exercise 5-7 | Favorite Fruit
- ➤ Exercise 5-8 | Hello Admin
- ➤ Exercise 5-9 | No Users
- ➤ Exercise 5-10 | Checking Usernames|
- ➤ Exercise 5-11 | Ordinal Numbers
- ➤ Exercise 5-12 | Styling if statements
- ➤ Exercise 5-13 | Your Ideas
π Connect With Us:
“Programs must be written for people to read, and only incidentally for machines to execute.”
— Harold Abelson
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment