SHARE
TWEET

Untitled

a guest Jul 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def get_source_base():
  2.     while (True):
  3.         try:
  4.             sourceBase = int(input("Please enter the base of the number you will convert: "))
  5.         except ValueError:
  6.             print("You have not entered a valid integer. Please try again.")
  7.             continue
  8.         if (sourceBase == 2 or sourceBase == 8 or sourceBase == 10):
  9.             return str(sourceBase)
  10.         else:
  11.             print("You have not entered a valid base. Valid bases are 2, 8 and 10. Please try again.")
  12.  
  13.  
  14. def get_target_base():
  15.     while (True):
  16.         try:
  17.             targetBase = int(input("Please enter the target base where the number will be converted: "))
  18.         except ValueError:
  19.             print("You have not entered a valid integer. Please try again.")
  20.             continue
  21.         if (targetBase == 2 or targetBase == 8 or targetBase == 10):
  22.             return str(targetBase)
  23.         else:
  24.             print("You have not entered a valid base. Valid bases are 2, 8 and 10. Please try again.")
  25.  
  26.  
  27. def get_number(sourceBase):
  28.     a = True
  29.     while (a):
  30.         try:
  31.             number = int(input("Please enter the number to be translated: "))
  32.         except ValueError:
  33.             print("You have not entered a valid integer. Please try again.")
  34.             continue
  35.  
  36.         number = str(number)
  37.         listNumber = list(number)
  38.  
  39.         if (sourceBase == "10"):
  40.             for i in listNumber:
  41.                 if i <= "9":
  42.                     a = False
  43.                     return number
  44.  
  45.                 else:
  46.                     print(sourceBase, "You have not entered a valid number in Base(", i, ">=", sourceBase,
  47.                           ").Please try again")
  48.                     break
  49.  
  50.         elif (sourceBase == "2") or (sourceBase == "8"):
  51.             s = "i"
  52.  
  53.             for i in listNumber:
  54.                 if (i >= sourceBase):
  55.                     s = "sikinti"
  56.                     print(sourceBase, "You have not entered a valid number in Base(", i, ">=", sourceBase,
  57.                           "). Please try again")
  58.                     break
  59.  
  60.             if (s != "sikinti"):
  61.                 a = False
  62.                 return number
  63.  
  64.  
  65. def convert_number(sourceBase, targetBase, number):
  66.     if (sourceBase == "2" and targetBase == "10") or (sourceBase == "8" and targetBase == "10"):
  67.         onluk = convert_base_to_10(number, sourceBase)
  68.         return onluk
  69.     elif (sourceBase == "2" and targetBase == "8") or (sourceBase == "8" and targetBase == "2"):
  70.         onluk = convert_base_to_10(number, sourceBase)
  71.         f = convert_10_to_base(onluk, targetBase)
  72.         return f
  73.     else:
  74.         f = convert_10_to_base(number, targetBase)
  75.         return f
  76.  
  77.  
  78. def convert_base_to_10(number, base):
  79.     if (base == "2" or base == "8"):
  80.         intBase = int(base)
  81.         intNumber = str(number)
  82.         liste = list(intNumber)
  83.         total = 0
  84.         size = len(liste)
  85.         for i in liste:
  86.             i = int(i)
  87.             total += i * (intBase ** (size - 1))
  88.             size -= 1
  89.         return total
  90.  
  91.  
  92. def convert_10_to_base(number, targetBase):
  93.     if (targetBase != "10"):
  94.         a=""
  95.         intNumber = int(number)
  96.         intTargetBase = int(targetBase)
  97.         while (intNumber > 0):
  98.             kalan = intNumber % intTargetBase
  99.             kalan=str(kalan)
  100.             a=kalan+a
  101.             intNumber //= intTargetBase
  102.         return a
  103.  
  104.  
  105. a = get_source_base()
  106. b = get_target_base()
  107. c = get_number(a)
  108. d = convert_base_to_10(c, a)
  109. e = convert_10_to_base(c, b)
  110. f = convert_number(a, b, c)
  111. print(f)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top