Simple Python program using bin
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 Code:
Enter an integer: 9000 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:
>>> |
Code:
#!/usr/local/bin/python3.1 |
Hi -
I happen to have Python 2.x and, as you're aware, "bin()" is new to Python 3. BUT .... 1. You APPEAR to be doing everything right 2. I suspect the actual problem is with "input()", not "bin()" SUGGESTIONS: 1. Declare "x" as global, initialize it, and print "x" before and after calling the function. 2. Try casting: Code:
x = int(input("enter an integer: ")) |
Thank you both. I caught the local variable and fixed it, and also tried Berhanie's code. Everything appears to be in order. :)
|
All times are GMT -5. The time now is 12:41 PM. |