Python Coding Question: ==================== The idea is to make use of while loops to check the user's input and only e

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Python Coding Question: ==================== The idea is to make use of while loops to check the user's input and only e

Post by answerhappygod »

Python Coding Question:
====================
The idea is to make use of while loops to check the user's input
and only end the while loop when a valid username or password have
been given. If a user name or password fails, tell the user why it
failed and ask them to try again.
Valid Username
A username is valid if it adhears to the following criteria:
Valid Password
A password is valid if it adhears to the following criteria:
Example Output
An expample run of the program could be:
Hello! Please register a username and password.
username:
- Must start with a letter.
- Must only contain letters and numbers.
- Must be at least 6 characters long.
Enter your username: gollum!
Invalid username: gollum!
- Must only contain letters and numbers.
Enter your username: gollum
password:
- Must contain at least one capital letter.
- Must contain at least one number.
- Must be at least 10 characters long.
Enter your password: My precious ring!
Invalid password: My precious ring!
- Must contain at least one number.
Enter your password: My precious 1ring!
Thank you, you have been registered as:
username: gollum
password: My precious 1ring!
Finish the Code
in main.py most of the code has
already been written, but you'll notice places with 'TODO' in the
comments. You must finish writting the code in these places.
CheckPassword(password)
This function has been declared, but you need to write the
implementation.
Use CheckUsername(username) as an
example. Feel free to make use of string helper functions to
validate the input in your code. For example, you might consider
using any
of: len(), isalpha(), isnumeric(), isupper(), isalnum(),
etc.
This function should return the string message for the criteria
that failed. It should not return anything if all checks pass
(returns None).
GetValidInput(input_type)
This function has also been declared, but you need to write the
implementation. Specifically, you'll need to add a While Loop to
retrieve user input that loops until a valid input is received. The
code should output to the user why an input failed, as they try
again.
The input_type can be either 'username' or
'password', so the function can know how to check the input
accordingly. Return the validated username or password string.
==============================
My code so far:
==============================
username_criteria = [
"- Must be at least 6 characters long.",
"- Must only contain letters or numbers.",
"- Must start with a letter.",
]
password_criteria = [
"- Must be at least 10 characters long.",
"- Must contain at least one number.",
"- Must contain at least one capital letter.",
]
def CheckUsername(username):
# Must be at least 6 characters
if len(username) < 6:
return username_criteria[0]
# Must only contain letters or numbers
if not username.isalnum():
return username_criteria[1]
# Must start with a letter
if not username[0].isalpha():
return username_criteria[2]
def CheckPassword(password):
# TODO: Check that the password is valid according to
the criteria. See CheckUsername for an example.
pass
def GetValidInput(input_type):
# TODO: Write code using a While Loop to get
validated username or password input.
pass
def GetInput(input_type):
if input_type == "username":
criteria = username_criteria
else:
criteria = password_criteria
print(input_type + ":")
for message in criteria:
print(message)
return GetValidInput(input_type)
print("Hello! Please register a username and password.")
print()
username = GetInput("username")
print()
password = GetInput("password")
print()
print("Thank you, you have been registered as:")
print("username: " + str(username))
print("password: " + str(password))
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply