palindrome_tutorial_fr.py

Created by 546f6d

Created on January 24, 2025

1.21 KB

ddd


def inverser_chaine(chaine):
    chaine_inversee = ""
    for caractere in chaine:
        chaine_inversee = caractere + chaine_inversee
    return chaine_inversee

# Fonction pour vérifier si un nombre est un palindrome
def is_palindrome(number):
    str_number = str(number)
    return str_number == inverser_chaine(str_number)

def find_palindrome(objectif:int, display_output:bool=False):
  count = 0
  number = 0
  
  while count < objectif:
    if is_palindrome(number):
      count += 1
      if display_output:
        print(str(count)+": "+str(number))
    number += 1
  return number - 1

# -------

print("")

while True:
    try:
        palindrome_objective = int(input("Quel n-ième nombre palindrome \nvoudriez-vous trouver ? "))
        break
    except ValueError:
        print("\nOups ! Ce n'était pas \nun nombre valide. \nEssayez encore...\n")

print("")

a = input("Vouliez-vous afficher \nchaque nombre palindrome \ntrouvé? (y/n default=n): ")
if a == "y":
  output = True
else:
  output = False


palindrome = find_palindrome(palindrome_objective, output)
    
# Afficher le résultat
print("\nLe", str(palindrome_objective) + "ème nombre palindrome\n est :", str(palindrome))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.