Focus test Key
1. # Prompt the user to enter their age
age = int(input("Please enter your age: "))
# Check if the user is eligible to vote
if age >= 18: print("You are eligible to vote.")
else: print("You are not eligible to vote.")
2. import random
import string
def generate_password(length, include_special_chars):
# Define character sets
letters = string.ascii_letters
digits = [Link]
special_chars = [Link]
# Combine character sets based on user preference
if include_special_chars:
chars = letters + digits + special_chars
else: chars = letters + digits
# Generate password
password = ''.join([Link](chars) for _ in range(length))
return password
3. user_input = int(input(prompt))
# Check if the input is within the specified range
if min_value <= user_input <= max_value:
return user_input else:
print(f"Input must be between {min_value} and {max_value}. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
4. def evaluate_boolean_expression(expression):
# Dictionary to store the boolean operations and their corresponding lambdas
operations = {
'and': lambda x, y: x and y,
'or': lambda x, y: x or y,
'not': lambda x: not x,
'==': lambda x, y: x == y,
'!=': lambda x, y: x != y,
'>': lambda x, y: x > y,
'<': lambda x, y: x < y,
'>=': lambda x, y: x >= y,
'<=': lambda x, y: x <= y }
# Split the expression by spaces
tokens = [Link]() try:
if len(tokens) == 2 and tokens[0] == 'not':
# Evaluate 'not' operation result = operations['not'](eval(tokens[1]))
elif len(tokens) == 3:
# Evaluate binary operations
left_operand = eval(tokens[0])
operator = tokens[1]
right_operand = eval(tokens[2])
if operator in operations:
result = operations[operator](left_operand, right_operand)
else: return "Invalid operator"
else: return "Invalid expression format"
return result
5. def main():
# Get three numbers from the user
num1 = get_number("Enter the first number: ")
num2 = get_number("Enter the second number: ")
num3 = get_number("Enter the third number: ")
# Calculate the average
total = num1 + num2 + num3
if total:
# This check isn't necessary for summation, but added for the structure of if-else
average = total / 3
print(f"The average of the three numbers is: {average}")
else:
print("There was an error in calculating the average.")
# Call the main function if __name__ == "__main__": main()
6. integer = int(input("Enter an integer: "))
# Convert the integer to a string
integer_as_string = str(integer)
# Display the result
print(f"The integer as a string is: '{integer_as_string}'")
7. def determine_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else: return 'F'
score = float(input("Enter the student's score (0-100): "))
# Check if the score is within the valid range
if 0 <= score <= 100:
# Determine the grade
grade = determine_grade(score)
# Display the grade
print(f"The student's grade is: {grade}")
else:
print("Invalid score. Please enter a score between 0 and 100.")
8. number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(f"The number {number} is even.")
else:
print(f"The number {number} is odd.")
9. # Prompt the user to enter some input
user_input = input("Enter something: ")
# Display the user input
print(f"You entered: {user_input}")
10. Substring =”hello”
String=”hello friends”
11. Len : Returns the number of items in an object (e.g., characters in a string, elements
in a list).
Upper() : Converts all characters in a string to uppercase.
12. if operation == '+':
return num1 + num2
elif operation == '-':
return num1 - num2
elif operation == '*':
return num1 * num2
elif operation == '/':
if num2 != 0:
return num1 / num2
else:
return "Error: Division by zero is not allowed."