0

Basically i made this program to practice python ( i am a complete noob at it), i am quite enjoying python, as my first programming langauge ever learnt or in the process of i feel very accomplished when completeing a program that works ( even if it is hello world). So anyways, i made a small program using techniques i had learnt from books and stuff from the internet and i have an issue, the program works fine, without problems but at the end there is a division where it justr goes wrong, it cannot divide anything unless it makes a whole number (eg. 100/20=5 but if i did 20/100 it would equel 0 and not 0.2), this also effects it if the number is going to be negative it just panics. i tried 15/20 to see if it was rounding but it still said 0.Any help would be fantastic ^_^ here is the code:

a=100
b=50
c=10
z=110
o=5
zoo=z+o+o

print "What is the value of zoo if:"
print "z=110"
print "o=5"
print "zoo=z+o+o"
import time
time.sleep(5)
print zoo,"of course!"

import time
time.sleep(1)

print "Wait..",a+b-(c)*3,"is the same as zoo except we just did it there using other code!"
import time
time.sleep(3)
print "We did it using 100+50-(10)*3 which then adds to zoo or 120!"

import time
time.sleep(3)

print "were gonna try something fun now!"
import time
time.sleep(2)

print "Please pick a number:"
number=int(raw_input())

print "and another:"
another=int(raw_input())

print "the two numbers you chose multiplied together makes",number*another
import time
time.sleep(2)
print "ok now were going to take your two numbers and divide them"
print "Your two numbers divided=",number/another
import time
time.sleep(1)
print "Ok im bored now, im going to go, have a nice day ^_^"

and here is the awnser with a problem:

What is the value of zoo if:
z=110
o=5
zoo=z+o+o
120 of course!
Wait.. 120 is the same as zoo except we just did it there using other code!
We did it using 100+50-(10)*3 which then adds to zoo or 120!
were gonna try something fun now!
Please pick a number:
15
and another:
20
the two numbers you chose multiplied together makes 300
ok now were going to take your two numbers and divide them
Your two numbers divided= 0
Ok im bored now, im going to go, have a nice day ^_^

oh and im on python 2.7.6

CheesyPi
  • 1
  • 1
  • 1
    You are dividing integers, so it will return the answer as an Integer. Integers are Whole Numbers(no decimals). If you do 15.0/20.0 you will get the correct answer as 15.0 is not an Integer but a float – stackErr Mar 16 '14 at 18:04

5 Answers5

0

Add above this line:

print "Your two numbers divided=",number/another

this code:

number, another = number + .0, another + .0

The reason your code doesn't work is because you're using int's. When you divide with integers, they return an integer or a whole number. You need to convert the numbers to floats by adding .0 to the numbers. This will allow you to get absolute division results.

Remolten
  • 2,614
  • 2
  • 25
  • 29
0

15/20 = 0 when performing an integer division since the result is less than 1. Therefore it truncates to 0.


// is used for dividing integers and / for floats- you are using the wrong operator so you get an incorrect result:

>>> 15 / 20
0
>>> 15 // 20
0.75

You can fix this by adding from from __future__ import division to your script. This will always perform a float division when using the / operator and use // for integer division- so just do what you are doing and it will return the expected result:

>>> from __future__ import division
>>> 15 / 20
0.75

I would use the above solution, with the import; but there are other ways. Another option would be making at least one of the operands a float, e.g. float(number) / another.

>>> number = 15
>>> another = 20
>>> float(number) / another
0.75

The above works because the result of the division depends on value types being used, in Python.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

You can add

from __future__ import division

at the top of your file. Then the default division strategy will be what you expect, i.e. floating point division. Python 2.7 does integer division by default.

Ronny Andersson
  • 1,558
  • 13
  • 12
  • Why is this here when my answer makes exactly the same point and it was posted before? This is just a duplicate... – anon582847382 Mar 16 '14 at 18:21
  • So you saying that I should wait until everybody else has written an answer before I suggest one? When I started writing my answer no one suggested the ´__future__´ statement as a potential solution to the problem. So I wrote that answer. You post an answer a minute before me and I don't keep hitting the refresh button in my browser while I am writing because I am busy writing the answer. And if you are concerned about duplicates, how about http://stackoverflow.com/q/21768286/2780199 or http://stackoverflow.com/q/1282945/2780199 or http://stackoverflow.com/q/1267869/2780199. – Ronny Andersson Mar 16 '14 at 18:39
  • While you are writing an answer, you don't need to refresh the page. There is a notification saying you should load the new answers if and when they are posted. It's just a way of providing clear solutions to visitors of the site- avoiding duplicate answers. – anon582847382 Mar 16 '14 at 18:40
  • Maybe you should focus your frustration on duplicate _questions_ instead of duplicate _answers_. I see no problem at all with multiple answers essentially saying the same thing, we all explain things slightly different. The scoring system will tell if an answer is good or not. – Ronny Andersson Mar 16 '14 at 18:55
0

The / quotient of two int's, in Python 2.x, is an int.

The / quotient of one int and one float, in Python 2.x, is a float.

The / quotient of two floats, in Python 2.x, is a float.

The / quotient of two int's, in Python 3.x, is a float.

The / quotient of one int and one float, in Python 3.x, is a float.

The / quotient of two floats in Python 3.x, is a float.

The // quotient of two int's, in Python 3.x, is an int.

The // quotient of one int and one float, in Python 3.x, is a whole-number float.

The // quotient of two floats in Python 3.x, is a whole-number float.

In Python 2.x, you can "from __future__ import division" at the top of your module, to get the 3.x behavior.

So since you're using 2.x, you probably should either "from __future__ import division" at the top of your module, or convert one or both of your int's to float with float(int_var) prior to / division.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
0

To add to all answers. In some languages (including python) division operator result depends on value types being used e.g.:

>>> 1 / 2 # integer divided by integer
0
>>> 1.0 / 2 # float divided by integer
0.5
aisbaa
  • 9,867
  • 6
  • 33
  • 48