Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #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
- def IsInt(variable):
- try:
- int(variable)
- return True
- except ValueError:
- return False
- #Fonction nous servant à inverser les digits d'un int
- def reverse_int(nombre):
- return int(str(nombre)[::-1])
- #Fonction permettant de convertir du decimal au shadok
- def decimal_a_shadok(nombre_decimal):
- quotient = nombre_decimal #Variable qui contiendra le quotient de la division euclidienne par 4
- reste = 0 #Variable qui contiendra le reste de cette division.
- base_shadok = [] #Liste qui contiendra la conversion du nombre base 10 en base 4
- if quotient==0: #Cas spécial ou le quotient vaut 0
- base_shadok.append(0) #On insère 0 dans la liste du nombre Shadok
- else:
- while quotient > 0:
- quotient = nombre_decimal // 4
- reste = nombre_decimal % 4
- nombre_decimal = quotient
- base_shadok.append(reste) #Le reste entre dans la liste du nombre Shadok
- base_shadok.reverse() #On met le mot dans le bon sens
- nombre_shadok = ["Ga","Bu","Zo","Meu"] #Liste contenant les lettres Shadok 0 = GA 1 = BU 2 = ZO 3 = MEU
- 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
- #Fonction permettant de convertir du Shadok à Décimal
- def shadok_a_decimal(mot_shadok):
- mot_shadok = mot_shadok.replace("Ga","0")
- mot_shadok = mot_shadok.replace("Bu","1")
- mot_shadok = mot_shadok.replace("Zo","2")
- mot_shadok = mot_shadok.replace("Meu","3") #On remplace les différents "digits Shadok" par leur équivalence en chiffres
- nombre_final_shadok = 0
- if IsInt(mot_shadok):
- mot_shadok = int(mot_shadok)
- mot_shadok = reverse_int(mot_shadok) #On inverse le mot
- for boucle in range(len(str(mot_shadok))):
- mot_shadok_conversion = mot_shadok
- puissance = int(str(mot_shadok_conversion)[boucle])*4**boucle
- nombre_final_shadok = nombre_final_shadok+puissance
- else: #Si le mot n'est pas en Shadok, on retourne -1
- nombre_final_shadok = -1
- return nombre_final_shadok
- while 1: #Boucle infinie
- patate = raw_input("Voulez vous convertir un nombre décimal à Shadok (1) ou un nombre Shadok à décimal (2) ?\n") #Choix du menu
- if IsInt(patate):
- if int(patate) == 1:
- nombre_decimal = raw_input("Veuillez entrer le nombre decimal a convertir en Shadok :\n")
- if IsInt(nombre_decimal): #Si la variable est bien un nombre
- nombre_shadok = decimal_a_shadok(int(nombre_decimal))
- print "En Shadok, " + str(nombre_decimal) + " devient " + str("".join(nombre_shadok)) + "\n" #On effectue et on affiche le résultat
- else:
- print str(nombre_decimal) + " n'est pas un nombre !\n" #Sinon il y'a conflit !
- elif int(patate) == 2:
- mot_shadok = raw_input("Veuillez entrer le mot en Shadok suivant ce modèle : GaBuZoMeu :\n")
- mot_shadok_origine = mot_shadok
- nombre_decimal = shadok_a_decimal(mot_shadok)
- if IsInt(nombre_decimal):
- if nombre_decimal == -1:
- print str(mot_shadok_origine) + " n'est pas un nombre en base Shadok, veuillez réesayer !\n"
- else:
- print "En Décimal, " + str(mot_shadok_origine) + " devient " + str(nombre_decimal) + "\n" #On effectue et on affiche le résultat
- else:
- print "Ce n'est pas en language Shadok ! Veuillez vérifier la saisie du texte\n"
- else:
- print "Veuillez choisir soit 1, soit 2, pas autre chose !\n"
- elif patate == 'patate':
- print("Voici un bel Easter Egg\n")
- else:
- print "Veuillez entrer un nombre !\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement