Série de TD 3 :
Exercise 1 : Manipulating lists
Write a program that takes a student's score (a number between 0 and 100) as input and determines the student's grade based on
the following criteria:
• A: Score is between 90 and 100 (inclusive).
• B: Score is between 80 and 89 (inclusive).
• C: Score is between 70 and 79 (inclusive).
• D: Score is between 60 and 69 (inclusive).
• F: Score is below 60.
Steps:
1. Ask the user to input their score (a number between 0 and 100).
2. Use an if-else if-else statement to assign the appropriate grade based on the score.
3. Print the grade
Exercise 2 : Find the First Even Number
Write a program that finds and prints the first even number from a list of integers. If the program encounters an even number,
it should print it and immediately stop the loop using a break statement.
Steps:
1. Create a list of integers (it can include both even and odd numbers).
2. Loop through the list of numbers.
3. As soon as you find an even number, print it and exit the loop using the break statement.
4. If no even numbers are found, print a message like "No even number found."
5. Add functionality to handle cases where the list is empty.
Exercise 3: "Print Multiples of 3, Skip Multiples of 5"
Write a program that iterates over a range of numbers (from 1 to 20) and prints the numbers that are multiples of 3, but skips the
numbers that are multiples of 5 using the continue statement.
Steps:
1. Loop through numbers from 1 to 20.
2. If the number is a multiple of 5, skip it using the continue statement.
3. If the number is a multiple of 3, print it.
4. Otherwise, do nothing (just continue to the next iteration).
Exercise: "Guess the Number"
Write a program that generates a random number between 1 and 100, and then allows the user to guess the number. The program should
1
Série de TD 3 :
give feedback whether the guess is too high, too low, or correct. It should continue prompting the user for guesses until the correct
number is guessed.
Steps:
1. Use a random number generator to pick a number between 1 and 100.
2. Continuously prompt the user to guess the number.
3. If the guess is too high, print "Too high! Try again."
4. If the guess is too low, print "Too low! Try again."
5. If the guess is correct, print "Congratulations! You've guessed the number."
6. The program should keep asking for guesses until the user guesses correctly. Use a while loop to continue asking until the
correct guess is made.
➢ You can use the [Link]() function (in Python, for example) to generate a random number between two values.
➢ Use a while loop to keep asking for guesses until the user guesses the correct number
➢ Limit the number of guesses (for example, allow the user only 7 attempts). If the user doesn't guess the correct number within
that limit, print a message like "Sorry, you've used all your attempts. The correct number was X."
7. Transform the while loop to for loop
Exercise 4: "Calculate the Factorial"
Write a function that calculates the factorial of a given number. A factorial of a number n (denoted as n!) is the product of all
positive integers from 1 to n. For example:
• 5! = 5 * 4 * 3 * 2 * 1 = 120
• 3! = 3 * 2 * 1 = 6
• 0! = 1 (by definition, the factorial of 0 is 1)
Steps:
1. Write a function called factorial(n) that takes a number n as input.
2. The function should calculate the factorial of the number n and return the result.
o Use a loop or recursion to compute the factorial.
3. The program should ask the user for a number and call the factorial() function to compute the result.
4. Display the calculated factorial.