Python - Homework - Converting Any Base to Any Base

前端 未结 6 1261
孤街浪徒
孤街浪徒 2020-11-28 09:53

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(         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 10:09

    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.

提交回复
热议问题