By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,404 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,404 IT Pros & Developers. It's quick & easy.

werid python troubles...

P: 27
Hello,
I am just having a little trouble getting python to work correctly. When I try to divide 10 by 3 I get 3 instead of 3.3333 or when I do 1/3 i get 0.
Expand|Select|Wrap|Line Numbers
  1. >>> 10/3
  2. 3
  3. >>> 1/3
  4.  
  5.  
Any ideas as to what is causing this and how i can fix it??
Sep 24 '10 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
You are seeing the results of integer division. Convert one of the numbers to float or import division from the __future__ module.
Expand|Select|Wrap|Line Numbers
  1. >>> 1/3
  2.  
  3. >>> from __future__ import division
  4. >>> 1/3
  5. 0.33333333333333331
  6. >>> float(1)/3
  7. 0.33333333333333331
  8. >>> 
Sep 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.