SIMULATION
Write a complete function password_strength(password) that returns "Strong" if the password is at least 8 characters long and contains both letters and numbers, "Weak" otherwise.
For example, password_strength("abc123def") should return "Strong".
def password_strength(password):
# TODO: Return "Strong" or "Weak" based on password criteria
if len(password) < 8:
return "Weak"
has_letter = False
has_number = False
for char in password:
if char.isalpha():
has_letter = True
elif char.isdigit():
has_number = True
# TODO: Add your return logic here based on has_letter and has_number
pass
==========
Step 1: First, check the password length using len(password).
Step 2: If the password has fewer than 8 characters, return 'Weak' immediately.
Step 3: Create two Boolean variables: has_letter and has_number.
Step 4: Loop through each character in the password.
Step 5: Use .isalpha() to check for letters and .isdigit() to check for numbers.
Step 6: If the password contains both at least one letter and at least one number, return 'Strong'.
Step 7: Otherwise, return 'Weak'.
Correct code:
def password_strength(password):
if len(password) < 8:
return 'Weak'
has_letter = False
has_number = False
for char in password:
if char.isalpha():
has_letter = True
elif char.isdigit():
has_number = True
if has_letter and has_number:
return 'Strong'
else:
return 'Weak'
Example:
print(password_strength('abc123def'))
print(password_strength('abcdefgh'))
print(password_strength('12345678'))
Output:
Strong
Weak
Weak
SIMULATION
Complete the function get_max(a, b) that returns the larger of two numbers. If they are equal, return either one.
def get_max(a, b):
# TODO: Return the larger of a and b
pass
==========
Step 1: The function receives two parameters: a and b.
Step 2: Compare the two values using an if statement.
Step 3: If a is greater than or equal to b, return a.
Step 4: Otherwise, return b.
Correct code:
def get_max(a, b):
if a >= b:
return a
else:
return b
Simplified correct code:
def get_max(a, b):
return max(a, b)
Example:
print(get_max(10, 20))
print(get_max(30, 15))
print(get_max(5, 5))
Output:
20
30
5
SIMULATION
Write a complete function calculate_average(grades) that takes a list of grades and returns the average as a float.
For example, calculate_average([85, 92, 78]) should return 85.0.
def calculate_average(grades):
# TODO: Calculate and return the average grade as a float
pass
==========
Step 1: The function receives one parameter named grades, which is a list of numbers.
Step 2: Use sum(grades) to add all the grades together.
Step 3: Use len(grades) to count how many grades are in the list.
Step 4: Divide the total by the number of grades.
Correct code:
def calculate_average(grades):
return sum(grades) / len(grades)
Example:
print(calculate_average([85, 92, 78]))
Output:
85.0
What sequence of steps is required to execute a Python script from a text editor using the terminal on a Windows device?
To run a Python script from a text editor using the terminal on Windows, the file should first be saved with the .py extension. Then, the terminal is opened, and the script is executed using the Python command followed by the filename.
Example:
python filename.py
The official Python documentation explains that when the interpreter is called with a filename argument, it reads and executes the script from that file. On Windows, the python command can be used from a terminal after Python is installed and configured correctly.
Therefore, the correct answer is A. Save file > open terminal > type python filename.py.
In Python, what must follow the in keyword in a for loop?
In a Python for loop, the in keyword is followed by an iterable object, such as a list, string, tuple, dictionary, set, or range() object.
Example:
for name in ['Alice', 'Bob', 'Carol']:
print(name)
Here, ['Alice', 'Bob', 'Carol'] is the iterable object. Python's documentation explains that a for statement iterates over the items of a sequence or other iterable object.
Therefore, the correct answer isB. An iterable object.
Mark Martinez
7 days agoJessica Robinson
27 days agoMaria Clark
1 month agoEmma Lopez
1 month agoGerald Clark
2 months agoMargaret Nelson
2 months agoJeffrey White
2 months agoMargaret Parker
3 months agoGeorge Nguyen
3 months ago