I\'m trying to make a program to convert a number in any base to another base of the user\'s choice. The code I have so far goes like this:
innitvar = float(
I need to multiply the leftmost digit in the initial number by its innitial base, and then add the next digit to the right, and then repeat until I hit the rightmost digit.
So you need to get digits. In a list.
Hint 1: Use divmod()
function to break a number into digits. Divide by 10 to get decimal digits.
Hint 2: While n > 0: you can use divmod()
to get a quotient and a remainder. If you save the remainder in the list, and use the quotient as the new value of n your number gets smaller until what's left is zero and you're done.
Hint 3: Your digits arrive in right-to-left order. Use reverse
to switch the order of the list of this bothers you. Or create the list by using insert(0,digit)
.
Now that you have the digits. In a list. You can iterate through the list.
Try the for
statement on for size.
You might need to use a "multiple and add" loop. total = total * new_base + next_digit
is the way the body of the loop often looks.