H
H
Hypick2021-09-29 22:36:05
Python
Hypick, 2021-09-29 22:36:05

How to make a decision when translating systems of calculation?

How to make a complete solution in this code as in the screenshots? When converting from decimal to any other system, you need to divide, and if vice versa, then multiply.

def convert_base(num, to_base=10, from_base=10):
    # first convert to decimal number
    n = int(num, from_base) if isinstance(num, str) else num
    # now convert decimal to 'to_base' base
    alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    res = ""
    while n > 0:
        n,m = divmod(n, to_base)
        res += alphabet[m]
    return res[::-1]


PxJHS.png
LhF5Cgx.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-09-29
@phaggi

If you just need to display the "ladder", then you need to format the string and output it in the while loop, for example, increasing the indentation each loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question