Advertisement
Dushy

Mini Projet ISN

Jan 22nd, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #Fonction verifiant si la variable est bien un entier, si la variable est bien un entier, alors la fonction retourne true, sinon elle donne False
  5. def IsInt(variable):
  6.     try:
  7.         int(variable)
  8.         return True
  9.     except ValueError:
  10.         return False
  11.  
  12. #Fonction nous servant à inverser les digits d'un int     
  13. def reverse_int(nombre):
  14.     return int(str(nombre)[::-1])
  15.  
  16. #Fonction permettant de convertir du decimal au shadok
  17. def decimal_a_shadok(nombre_decimal):
  18.     quotient = nombre_decimal                               #Variable qui contiendra le quotient de la division euclidienne par 4
  19.     reste = 0                                               #Variable qui contiendra le reste de cette division.
  20.     base_shadok = []                                        #Liste qui contiendra la conversion du nombre base 10 en base 4
  21.     if quotient==0:                                         #Cas spécial ou le quotient vaut 0
  22.         base_shadok.append(0)                               #On insère 0 dans la liste du nombre Shadok
  23.     else:
  24.         while quotient > 0:
  25.             quotient = nombre_decimal // 4                    
  26.             reste = nombre_decimal % 4                          
  27.             nombre_decimal = quotient                      
  28.             base_shadok.append(reste)                       #Le reste entre dans la liste du nombre Shadok
  29.     base_shadok.reverse()                                   #On met le mot dans le bon sens
  30.     nombre_shadok = ["Ga","Bu","Zo","Meu"]                  #Liste contenant les lettres Shadok 0 = GA 1 = BU 2 = ZO 3 = MEU
  31.     return [nombre_shadok[loop] for loop in base_shadok]    #Retourne une liste nombre_shadok associant à chaque valeur de nombre_shadok son equivalent de la liste base_shadok
  32.  
  33. #Fonction permettant de convertir du Shadok à Décimal
  34. def shadok_a_decimal(mot_shadok):                          
  35.    mot_shadok = mot_shadok.replace("Ga","0")
  36.    mot_shadok = mot_shadok.replace("Bu","1")
  37.    mot_shadok = mot_shadok.replace("Zo","2")
  38.    mot_shadok = mot_shadok.replace("Meu","3")               #On remplace les différents "digits Shadok" par leur équivalence en chiffres
  39.    nombre_final_shadok = 0
  40.    if IsInt(mot_shadok):
  41.       mot_shadok = int(mot_shadok)
  42.       mot_shadok = reverse_int(mot_shadok)                  #On inverse le mot
  43.       for boucle in range(len(str(mot_shadok))):
  44.          mot_shadok_conversion = mot_shadok
  45.          puissance = int(str(mot_shadok_conversion)[boucle])*4**boucle
  46.          nombre_final_shadok =  nombre_final_shadok+puissance
  47.    else:                                                    #Si le mot n'est pas en Shadok, on retourne -1
  48.       nombre_final_shadok = -1
  49.    return nombre_final_shadok
  50.  
  51. while 1:                                                                                                            #Boucle infinie
  52.     patate = raw_input("Voulez vous convertir un nombre décimal à Shadok (1) ou un nombre Shadok à décimal (2) ?\n")    #Choix du menu
  53.     if IsInt(patate):
  54.         if int(patate) == 1:
  55.             nombre_decimal = raw_input("Veuillez entrer le nombre decimal a convertir en Shadok :\n")
  56.             if IsInt(nombre_decimal):                                                                               #Si la variable est bien un nombre
  57.                 nombre_shadok = decimal_a_shadok(int(nombre_decimal))
  58.                 print "En Shadok, " + str(nombre_decimal) + " devient " + str("".join(nombre_shadok)) + "\n"                #On effectue et on affiche le résultat
  59.             else:
  60.                 print str(nombre_decimal) + " n'est pas un nombre !\n"                                              #Sinon il y'a conflit !
  61.         elif int(patate) == 2:
  62.            mot_shadok = raw_input("Veuillez entrer le mot en Shadok suivant ce modèle : GaBuZoMeu :\n")
  63.            mot_shadok_origine = mot_shadok
  64.            nombre_decimal = shadok_a_decimal(mot_shadok)
  65.            if IsInt(nombre_decimal):
  66.                if nombre_decimal == -1:
  67.                   print str(mot_shadok_origine) + " n'est pas un nombre en base Shadok, veuillez réesayer !\n"
  68.                else:
  69.                   print "En Décimal, " + str(mot_shadok_origine) + " devient " + str(nombre_decimal) + "\n"                #On effectue et on affiche le résultat
  70.            else:
  71.                print "Ce n'est pas en language Shadok ! Veuillez vérifier la saisie du texte\n"
  72.         else:
  73.             print "Veuillez choisir soit 1, soit 2, pas autre chose !\n"
  74.     elif patate == 'patate':
  75.         print("Voici un bel Easter Egg\n")
  76.     else:
  77.         print "Veuillez entrer un nombre !\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement