Skip to content

Instantly share code, notes, and snippets.

@PatWg
Last active October 3, 2023 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatWg/31fae97a3eef2e8a615f53fea36d4ecc to your computer and use it in GitHub Desktop.
Save PatWg/31fae97a3eef2e8a615f53fea36d4ecc to your computer and use it in GitHub Desktop.
ICC - Série 2 - Correction
'''
Exercice 1
Il s'agit de bien comprendre l'ordre des opéreations.
'''
a: bool = 4 + 2 < 1 # False
b: bool = not True and not False or False # False
c: bool = "epfl" != "EPFL" and not False # True
d: bool = "genève" > "lausanne" # False
'''
Exercice 2
Il s'agit de comprendre l'utilisation de la fonction input() avec ou sans
argument.
'''
print("Bonjour, veuillez saisir votre prénom: ")
first_name: str = input()
last_name: str = input("Veuillez saisir votre nom de famille: ")
print(f"Bonjour {first_name} {last_name}!")
'''
Exercice 3
Structures conditionnelles et utilisation de l'opérateur modulo
'''
mod_1: int = 5 % 2
mod_2: int = 18 % 7
mod_3: int = 24 % 8
print(mod_1)
print(mod_2)
print(mod_3)
year: int = 2023
# year: int = 2024
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(f"{year} est une année bissextile.")
else:
print(f"{year} n'est pas une année bissextile.")
'''
Exercice 4
I/O et conversion de types
'''
year_ex4: int = int(input("Saisir une année: "))
if (year_ex4 % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(f"{year_ex4} est une année bissextile.")
else:
print(f"{year_ex4} n'est pas une année bissextile.")
'''
Exercice 5
Boucles, manipulation de chaînes de caractères, et variables
'''
line: str = input("Veuillez taper quelque chose: ")
# for i in range(len(line)):
# print(line[i])
uppercase: int = 0
lowercase: int = 0
for char in line:
if char.islower():
lowercase += 1
elif char.isupper():
uppercase += 1
print(f"Majuscules: {uppercase}, Minuscules: {lowercase}.")
'''
Exercice 6
Boucles et manipulation de chaînes de caractères, suite
Cet exercice demande un peu plus de réflexion du point de vue de la modélisation
(il faut créer quelques variables en plus) et de l'algorithmique.
'''
course_title: str = "information, calcul, communication"
capitalized_course_title: str = ""
previous_char: str = ""
first_character: bool = True
for index, character in enumerate(course_title):
if first_character or previous_char == " ":
capitalized_course_title += character.upper()
else:
capitalized_course_title += character
first_character = False
previous_char = character
print(f"Cours: {capitalized_course_title}")
'''
Exercice 7
Calendrier, version finale
'''
day: int = int(input("Jour: "))
month: int = int(input("Mois: "))
year_ex7: int = int(input("Année: "))
nb_days_in_month: int = 0
new_month: bool = True
for i in range(0, 14, 1):
print(f"Le cours numéro {i+1} aura lieu le {day}.{month}.{year_ex7}.")
if new_month:
if month in (1, 3, 5, 7, 8, 10, 12):
nb_days_in_month = 31
elif month in (4, 6, 9, 11):
nb_days_in_month = 30
else:
if (year_ex7 % 4 == 0 and year_ex7 % 100 != 0) or year_ex7 % 400 == 0:
nb_days_in_month = 29
else:
nb_days_in_month = 28
new_month = False
if day + 7 > nb_days_in_month:
month += 1
new_month = True
day = (day + 7) - nb_days_in_month
else:
day += 7
'''
Exercice 8
Interpréteur interactif
Ne consulter que si vous avez pu faire cet exercice lors de la seconde séance d'exercices.
'''
should_continue: bool = True
while should_continue:
print("Je vais évaluer un calcul pour vous.")
number1_string: str = input("Tapez le premier nombre:")
number1: int = int(number1_string)
number2_string: str = input("Tapez le second nombre:")
number2: int = int(number2_string)
operation: str = input("Tapez l'opération: ")
if operation == "+" or operation == "addition":
result = number1 + number2
print(f"{number1} + {number2} = {result}")
elif operation == "-" or operation == "soustraction":
result = number1 - number2
print(f"{number1} - {number2} = {result}")
elif operation == "*" or operation == "multiplication":
result = number1 * number2
print(f"{number1} * {number2} = {result}")
elif operation == "/" or operation == "division":
if number2 == 0:
print(f"Division par zéro, calcul impossible.")
else:
result_as_float: float = number1 / number2
print(f"{number1} / {number2} = {result_as_float}")
else:
print(f"Désolé, je ne connais pas l'opération '{operation}'.")
maybe_quit: str = input("Tapez 'q' pour quitter, ou autre chose pour recommancer: ")
if maybe_quit == 'q':
print("Bye!")
should_continue = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment