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(raw_input("Please enter a number: "))
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
These are the data that I get from the user. The initial number, its initial base, and the base the user wants to convert to. As I understand it, I need to convert to base 10, and then to the desired base, specified by the user.
This is where I’m hitting a brick wall: I need to multiply the leftmost digit in the initial number by its initial base, and then add the next digit to the right, and then repeat until I hit the rightmost digit. I understand how to do this on paper, but I have no idea how to put it into Python code. I’m not sure how I would multiply the first number, and then add the next, nor do I understand how to let the program know when to stop performing this operation.
I’m not asking to have the program written for me, but I would like to be pointed in the right direction.
Thanks for your time!
This should be the first half of the answer to your problem. Can you figure out how to convert to a base?
Here is the second half of the solution. By using these two functions, converting bases is very easy to do.
After putting it all together, you should end up with the program below. Please take time to figure it out!