Python Forum

Full Version: multiple number format conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, please help! Below is a code I created for the conversion of decimal into binary. Now I want to expand the code so that the user can choose any number format( I only want to use base numbers 2-9 ). I'm a beginner, so don't quite know where to start here. I've tried everything from creating multiple lists to changing the append, but no luck! Any help, hints would be much appreciated. Thanks :)

number = 0
intermediateResult = 0
remainder = []
number = int(input("Please insert value for conversion: "))
while number != 0:
    remainder.append(number % 2)
    number = number // 2
remainder.reverse()
for result in remainder:
    print(result, end = "")
As it posted under 'General coding help' then I suggest using numpy.base_repr.

If it seems overkill to use numpy for simple conversion then using divmod makes code simpler (works for up to base 10):

def convert(integer, base):
    base_digits = []
    while integer:
        integer, base_digit = divmod(integer, base)
        base_digits.append(str(base_digit))
    return ''.join(reversed(base_digits))

# convert(100, 2) -> '1100100'
# convert(100, 3) -> '10201'
# convert(100, 8) -> '144'
# convert(100, 10) -> '100'
In case you need to know, the numpy function has limits. It can only convert between bases 2-36. I checked it out. But one liners are good and beautiful.
Thanks for the infos guys. Im not that far ahead yet though. I eventually got it after 8 hrs of staring at my white board. Hit the Wall
Only thing I haven't been able to do though is limit the target system for the conversion to the base numbers 2 to 9 only. Next challenge I guess ;)

def function():
    number = 0
    intermediateResult = 0
    remainder = []
    number = int(input("Enter number to be converted: "))
    base = int(input("Choose numerical format: "))
    while number != 0:
            remainder.append(number % base)
            number = number // base
            remainder.reverse()
    for result in remainder:
       print(result, end = "")

function()
Cheers
(Aug-10-2020, 05:36 PM)oli_action Wrote: [ -> ]Only thing I haven't been able to do though is limit the target system for the conversion to the base numbers 2 to 9 only.

I suggest to change name of the function to something meaningful and move user input out of the function.

In order to limit base you should set up user input validation, something like this:

def validate(request, allowed_range):
    range_text = f'in range {min(allowed_range)} - {max(allowed_range)}'
         
    while True:
        answer = input(f'{request} {range_text} ')
        try:
            answer = int(answer)
            if answer in allowed_range:
                return answer
            raise ValueError
         
        except ValueError:
            print(f'Expected base {range_text} but input was {answer} ')

# usage:

base = validate("Enter base to convert to", range(2, 10))