Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import math
  2.  
  3. def considerHex(letter):
  4. if letter == "A" or letter == "a":
  5. return 10
  6. if letter == "B" or letter == "b":
  7. return 11
  8. if letter == "C" or letter == "c":
  9. return 12
  10. if letter == "D" or letter == "d":
  11. return 13
  12. if letter == "E" or letter == "e":
  13. return 14
  14. if letter == "F" or letter == "f":
  15. return 15
  16. else:
  17. return int(letter)
  18.  
  19. def main():
  20. # retrieve an input from the user
  21. # if type isnt number output error and get input again
  22. # if type is number then store it and retrieve input again for the base of the number supplied
  23. # store the number for the base of the number supplied
  24. # ask the user for the base of the desired output nuymber
  25. # calculate the conversion and return it
  26.  
  27. originalNumber = input("Please input a number: ")
  28. originalBase = int(input("Please input the base of your number in numerical form: "))
  29. outputBase = int(input("Please input the base of the desired conversion: "))
  30. baseTen = 0
  31. maxExponent = 30
  32. outputNumber = 0
  33. for index, digit in enumerate(originalNumber):
  34. # add consideration for bases higher than 10
  35. digit = considerHex(digit)
  36. currentPlace = (len(originalNumber) - index - 1)
  37. baseTen += digit * (originalBase ** (len(originalNumber) - index - 1))
  38. print("your number in base 10 is %s" % baseTen)
  39. # now convert to output base
  40. while baseTen >= 0 and maxExponent >= 0:
  41. currentDigit = baseTen / (outputBase ** maxExponent)
  42. if 1 <= currentDigit < baseTen:
  43. multiplier = math.floor((baseTen / (outputBase ** maxExponent)))
  44. if multiplier == 0:
  45. multiplier = 1
  46. outputNumber += multiplier * (10 ** maxExponent)
  47. baseTen -= multiplier * (outputBase ** maxExponent)
  48. maxExponent -= 1
  49. # add the last trailing remainder of baseTen
  50. outputNumber += baseTen
  51. baseTen -= baseTen
  52. print("your number in base %s is %s" % (outputBase, outputNumber))
  53.  
  54.  
  55. if __name__ == '__main__':
  56. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement