Python Forum
multiple number format conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple number format conversion
#1
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 = "")
Reply
#2
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
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
Reply
#5
(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))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you format Update statement with multiple conditions hammer 4 2,038 Dec-16-2021, 10:49 PM
Last Post: hammer
Photo How can I use 2 digits format for all the number? plumberpy 6 2,299 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  stopping number conversion before end Skaperen 6 2,941 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  Money conversion - problems with lists and .format function fatherted99 1 1,782 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  Parquet format conversion problem Bilhardas 1 1,620 Nov-19-2019, 11:06 AM
Last Post: baquerik
  naming images adding to number within multiple functions Bmart6969 0 1,892 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Number format william888 3 2,790 Aug-23-2019, 05:33 AM
Last Post: ThomasL
  Changing Number Format moby 4 4,997 May-24-2019, 11:04 PM
Last Post: snippsat
  Date format conversion "/Date(158889600000)/" lbitten 2 2,789 Nov-29-2018, 02:14 PM
Last Post: Larz60+
  Counting the number of files related to particular format ambush 3 2,876 Nov-05-2018, 08:58 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020