-1

I am a little confused with how int() works when converting a number from base 10 to base 3 in Python.

This code will print out the converted base 3 numbers from the range given by the user.

Value=0
Number=int(input("Please Input A Number "))    
for i in range(0,Number):
    Value=int(i,base=3)
    print(Value)

I have TypeError: int() can't convert non-string with explicit base returned as an error.

An example of what I am trying to achieve. Example 32 in base 3 is 1012 Perhaps I am confused to what int(x,base=y) actually does to the number.

BubblesPop
  • 11
  • 3
  • 2
    You are converting the other way around. `int(i, 3)` converts a string representation of a base 3 number to base 10. For example, `int('e5e2', 16)` returns `58850`, which is the base 10 representation of `e5e2` – user3483203 Feb 08 '18 at 17:13
  • 3
    Possible duplicate of [How to convert an integer in any base to a string?](https://stackoverflow.com/questions/2267362/how-to-convert-an-integer-in-any-base-to-a-string) – Ma0 Feb 08 '18 at 17:18
  • So this code will not convert the integer into another integer? Example 32 in base 3 is 1012 – BubblesPop Feb 08 '18 at 17:26
  • @BubblesPop No, it will not. The error message is very explicit about it as well as the docstring. – Stop harming Monica Feb 08 '18 at 18:17

3 Answers3

0

The internal representation of numbers is base-less. You can only convert strings with a given base to numbers:

>>> int("21", 3)
7
>>> bin(int("21", 3))
'0b111'
>>> hex(int("21", 3))
'0x7'
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • This is correct, but I think he is confused about which way the conversion is going in this case. It seems like he wants to convert from base 10 to base 3. – user3483203 Feb 08 '18 at 17:15
  • So this code will not convert the integer into another integer? Example 32 in base 3 is 1012 – BubblesPop Feb 08 '18 at 17:33
0

I think your issue comes from the data type of i. According to the documentation in Python 2.7 and 3.6, the value you wish to convert must be a string when a base is provided.

"If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base."

Therefore, in order for your code to work, you must convert the value of i to a string.

    int((str(i)), 3)
0

You are trying to convert a number from Base 10 to Base 3, which is not what int() does. From the documentation:

class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given.

It does not convert to other bases, it converts from the specified base to an integer.

If you want to convert a number from Base 10 to Base 3, you can use the following, found here. I slightly adapted it for readability, so it only converts to Base 3.

def baseThree(num, numerals="012"):
    return ((num == 0) and numerals[0]) or (baseThree(num // 3, numerals).lstrip(numerals[0]) + numerals[num % 3])

print(baseThree(32))

Output:

1012

Adapted for your specific question:

num = int(input("Please Input A Number "))
for i in range(0, num):
     print(baseThree(i))

Output with num = 5:

Please Input A Number 5
0
1
2
10
11
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • Thanks! When I saw that int() had a base parameter, i thought that it would simply convert the number. Can I ask where you would use int() with the base parameter? – BubblesPop Feb 09 '18 at 18:10
  • You can use it to convert strings representations of numbers in other bases to base 10. For example, `int('101', 2)` returns `5` – user3483203 Feb 09 '18 at 18:23