I am exploring the Python 3 standard library and am currently attempting to test the
bin function. It converts an integer into a binary string.
I believe the module I wrote is flawed somehow. Here's the source code:
Code:
#!/usr/bin/python3.1
#This module tests the bin() function.
import sys
def get_input():
x = input("Enter an integer: ")
def use_bin():
print("The binary form of this integer is ", bin(x))
get_input()
use_bin()
And here's the output in the interpreter:
Code:
Enter an integer: 9000
The binary form of this integer is 0b10111
>>>
Enter an integer: 2
The binary form of this integer is 0b10111
>>>
As you can see, the binary form given is always 0b10111. I'm no expert on binary code (or hexadecimal notation), but surely 9000 and two would have different results?
EDIT: Added a line in the module to repeat back what integer the user entered, and then the binary form. It would appear that no matter what integer the user enters, Python thinks it's "23".
Example output:
Code:
>>>
Enter an integer: 1
You entered 23
The binary form of this integer is 0b10111
>>>