Lesson 20 of 21 · 2 min · practice
Practice Questions
Q1. Write a grade calculator. Ask the user for a score (0-100). Print the grade: A (90+), B (80-89), C (70-79), D (60-69), F (below 60). Also print "Excellent!" if grade is A, and "Need improvement" if grade is F.
Q2. Write a program using a for loop and range() to print all multiples of 7 between 1 and 100 (inclusive). Also print the total count and their sum.
Q3. Use a while loop to simulate a number guessing game. The secret number is 42. Keep asking the user to guess until they get it right. After each wrong guess, tell them if their guess is too high or too low. Print how many guesses it took.
Q4. Using for and continue, print all numbers from 1 to 50 that are NOT divisible by 3 or 5. Count how many such numbers exist.
Q5. Write a program that uses a nested for loop to print a star pattern:
* ** *** **** *****
Q6. Using a list comprehension, create a list of all numbers from 1 to 100 that are divisible by both 3 and 7 (i.e., divisible by 21). Then print the list and its length.
Q7. You have a list of scores: [85, 92, 45, 78, 65, 55, 90, 38, 72, 88]. Using loops and conditions: (a) count how many students passed (score >= 60), (b) find the highest score, (c) find the lowest score, (d) calculate the class average.
Q8. Write a program that asks the user to enter numbers one at a time. The program should keep a running total and count. When the user types "done", print the total and average of all numbers entered. Use a while loop.
Q9. Explain the difference between these two loops and what each prints:
# Loop A
for i in range(10):
if i == 5:
break
print(i)
# Loop B
for i in range(10):
if i == 5:
continue
print(i)
Q10. Using a list comprehension, create a dictionary where keys are student names and values are their grades (Pass/Fail) given this list of (name, score) tuples: students = [("Priya", 85), ("Ahmed", 45), ("Ravi", 72), ("Sara", 58), ("Meera", 91)]. A student passes if score >= 60.
