LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2010, 05:52 PM   #1
eveningsky339
Member
 
Registered: Mar 2010
Location: Western Maine
Distribution: PCLinuxOS (LXDE)
Posts: 466

Rep: Reputation: 51
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

#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
>>>

Last edited by eveningsky339; 07-31-2010 at 06:15 PM.
 
Old 07-31-2010, 07:26 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Code:
#!/usr/local/bin/python3.1


def get_input():
        # don't put the result in a local variable
        return input("Enter an integer:  ")

def use_bin(x):
        print("The binary form of this integer is ", bin(int(x)))

n = get_input()
use_bin(n)
 
Old 07-31-2010, 07:28 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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: "))
'Hope that helps .. PSM

Last edited by paulsm4; 07-31-2010 at 07:30 PM.
 
Old 07-31-2010, 07:46 PM   #4
eveningsky339
Member
 
Registered: Mar 2010
Location: Western Maine
Distribution: PCLinuxOS (LXDE)
Posts: 466

Original Poster
Rep: Reputation: 51
Thank you both. I caught the local variable and fixed it, and also tried Berhanie's code. Everything appears to be in order.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
indentation error in python simple socket program xskycamefalling Programming 1 02-02-2010 07:15 PM
Python CGI-Bin Scripts chaney44145 Linux - Server 1 01-15-2008 11:44 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
Python: Need help making a simple program using arrays and loops Baix Programming 11 08-13-2005 11:26 PM
openGL ,python and .bin linker problem in mdk9.1 ngan_yine Mandriva 0 12-14-2003 11:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration