Questions And Answers, Questions with Solutions
Newest 2026-2027 | Already Graded A+
Q1. Write a complete function calculate_discount(price, discount_percent) that
calculates and returns the final price after applying a discount percentage.
Example: calculate_discount(75, 20) → 60.0
python
def calculate_discount(price, discount_percent):
# TODO: Calculate and return the final price after discount
pass
Answer:
python
def calculate_discount(price, discount_percent):
return price * (1 - discount_percent / 100)
Q2. Write a function password_strength(password) that returns "Strong" if the
password is at least 8 characters long and contains both letters and
numbers, "Weak" otherwise.
Example: password_strength("abc123def") → "Strong"
python
def password_strength(password):
# TODO: Return "Strong" or "Weak"
pass
Answer:
,python
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
return "Strong" if has_letter and has_number else "Weak"
Q3. Write a function is_even(n) that returns True if the integer n is
even, False otherwise.
Example: is_even(4) → True
python
def is_even(n):
# TODO: Return True if n is even
pass
Answer:
python
def is_even(n):
return n % 2 == 0
,Q4. Write a function max_of_three(a, b, c) that returns the largest of three
numbers.
Example: max_of_three(10, 25, 15) → 25
python
def max_of_three(a, b, c):
# TODO: Return the maximum value
pass
Answer:
python
def max_of_three(a, b, c):
return max(a, b, c)
Q5. Write a function factorial(n) that returns the factorial of a non-negative
integer n using recursion.
Example: factorial(5) → 120
python
def factorial(n):
# TODO: Return n! recursively
pass
Answer:
python
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
, Q6. Write a function reverse_string(s) that returns the reversed version of the input
string s.
Example: reverse_string("hello") → "olleh"
python
def reverse_string(s):
# TODO: Return reversed string
pass
Answer:
python
def reverse_string(s):
return s[::-1]
Q7. Write a function count_vowels(s) that returns the number of vowels (a, e, i, o,
u) in a string s (case-insensitive).
Example: count_vowels("Hello World") → 3
python
def count_vowels(s):
# TODO: Count vowels in s
pass
Answer:
python
def count_vowels(s):
vowels = "aeiou"
return sum(1 for char in s.lower() if char in vowels)