Help with personal python project (converting base 10 to other bases)

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at. The query string (? and beyond) may be truncated.>

<In what way does your code behave incorrectly? Include ALL error messages.>
I am trying to make a program which converts base 10 numbers to other bases of the user’s choice. The variable base_number is the user’s variable, and base_base is the user’s choice of which base to convert to.

There is a way to convert to different bases by dividing the base 10 number by the base and taking the remainder and repeating until it is not divisible and write the quotient followed by all the remainders. (EX. 50/6 = 8 R 2, 8/6 = 1 R2 so 50 in base 6 would be 122).

I created a list (remainder) so that it would append the remainder and gather all the remainders and get the quotient later and put it in order to get the user’s base number.
<What do you expect to happen instead?>

Thanks for looking at the code and helping me out!



def base():
    base_number = raw_input("Please enter a number in base 10")
    base_base = raw_input("What base would you like to convert it to?")
    
    base_number = int(base_number)
    base_base = int(base_base)
    while (base_number / base_base) > base_base:
        remainder = []
        remainder.append(base_number % base_base)
        print remainder
    if base_number <= base_base:
        print 'Your number in base ' + str(base_base) + ' is ' + str(base_number)
    
base()
        
        


1 Like

Just working from your theory here is a proof of concept; but, it has limitations…

def convert_base(number, base):
    if base < 2:
        return False
    remainders = []
    while number > 0:
        remainders.append(str(number % base))
        number //= base
    remainders.reverse()
    return ''.join(remainders)

print convert_base(255, 2)    # 11111111

print convert_base(25, 8)     # 31

Update:

Took it a little further

Code
'''
http://stackoverflow.com/questions/16060899/alphabet-range-python

Number base study by Alex Shetye & Roy

https://discuss.codecademy.com/t/help-with-personal-python-project-converting-base-10-to-other-bases/39218
'''
import string

CHAR_LOOKUP = list(string.digits + string.ascii_uppercase)

def numeral(n):
    global CHAR_LOOKUP
    return CHAR_LOOKUP[n]

def convert_base(number, base):
    if base < 2 or base > 36:
        return False
    mods = []
    while number > 0:
        mods.append(numeral(number % base))
        number //= base
    mods.reverse()
    return ''.join(mods)

print convert_base(255, 2)
print convert_base(25, 8)
print convert_base(65535, 16)
print convert_base(19, 20)
print convert_base(56784323, 36)
print convert_base(35, 36)

and added numeral representation for up to base 36.

import string

CHAR_LOOKUP = list(string.digits + string.ascii_uppercase)

def numeral(n):
    global CHAR_LOOKUP
    return CHAR_LOOKUP[n]

def convert_base(number, base):
    if base < 2 or base > 36:
        return False
    mods = []
    while number > 0:
        mods.append(numeral(number % base))
        number //= base
    mods.reverse()
    return ''.join(mods)

print convert_base(255, 2)       # 11111111
print convert_base(25, 8)        # 31
print convert_base(65535, 16)    # FFFF
print convert_base(19, 20)       # J
print convert_base(56784323, 36) # XT32B
print convert_base(35, 36)       # Z

Base 36 can handle huge numbers. In just 8 characters it represents,

convert_base(2821109907455, 36)  # 'ZZZZZZZZ'

What would this look like in binary?

import math

print math.log(2821109907456) / math.log(2)   # 41.359400011538497

That means this number falls between 2 ** 41 and 2 ** 42. Imagine how big the number when it is 32 characters… That would be 168 bits!

 > convert_base(2821109907455, 2)
=> '101001000011010111010000001111111111111111'

This is a fun topic, and worth exploring, both from a programming and a math perspective.

https://www.google.ca/search?q=named+number+bases

Update:

Now we have base 62 capability as long as HEX is all caps. This lets us create numbers in the range of 10 to the 18th in magnitude. The maximum value falls somewhere in this range. I stil have not figured it out yet, but I did add some spunk to the program.

Code
'''
http://stackoverflow.com/questions/16060899/alphabet-range-python

Number base study by Alex Shetye & Roy

https://discuss.codecademy.com/t/help-with-personal-python-project-converting-base-10-to-other-bases/39218
'''
import string
import random

CHAR_LOOKUP = list(string.digits + string.ascii_uppercase + string.ascii_lowercase)

def numeral(n):
    global CHAR_LOOKUP
    return CHAR_LOOKUP[n]

def convert_base(number, base):
    if base < 2 or base > 62:
        return False
    mods = []
    while number > 0:
        mods.append(numeral(number % base))
        number //= base
    mods.reverse()
    return ''.join(mods)
'''
print convert_base(255, 2)
print convert_base(25, 8)
print convert_base(65535, 16)
print convert_base(19, 20)
print convert_base(56784323, 36)
print convert_base(35, 36)
print convert_base(840223214528973775, 62)
'''
print convert_base(int(1e17), 62)
print convert_base(int(1e18), 62)

for k in range(10):
    print convert_base(random.randrange(1e17), 62)

print "=========="
def getTen():
    r = convert_base(random.randrange(1e18), 62)
    return r if len(r) == 10 else getTen()
for i in range(10):
    print getTen()
 

Now we can create 10 character random strings just by throwig a big random number at the function.