-1

In following code, I always get the result as a closest integer. But I would like to have the result of the division as a float i.e. 12/5 = 1.4, not 2 which is what I get in the program. I am using python2.7

"""Given division of two numbers, the result will print out """
try:
    divident = int(raw_input("Enter the divident: "))
    divisor = int(raw_input("Enter the divisor: "))
    print (" %d devided by %d is %f: " % ( divident, divisor, divident / divisor))

except(ValueError, ZeroDivisionError):
    print ("Something went wrong!")
M J
  • 25
  • 4

3 Answers3

1

The basic explanation is that in almost all programming languages, dividing 2 variables of numeric type T returns a value of that type T. Integers division is performed by the processor as an euclidian division, returning the quotient (as an integer).

The print format %f will not perform the variable type conversion for you.

I strongly suggest you read the proposed duplicate question for further understanding of python behaviour.

Example:

12 = (2 * 5) + 2 => 12 / 5 = 2     12 % 5 = 2
12 = (1 * 7) + 5 => 12 / 7 = 1     12 % 7 = 5

In python :

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 12/5
2
>>> 12%5
2
>>> 12/7
1
>>> 12%7
5

if you want to obtain a float, do as https://stackoverflow.com/users/8569905/banghua-zhao proposed.

cast in float, and then perform a division. The processor will then using floating point division and return a float. As pointed out in a comment below, if 2 operands have a different type, the operator computation is performed with the most restrictive type : float will take precedence over integer. In the following examples, one cast to float would be sufficient.

>>> float(12)/float(5)
2.4

Note that the % operator still performs an euclidian divison and gives you a the result as a float

>>> float(12)%float(5)
2.0
>>> float(12)%float(7)
5.0
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
0

You divident and divisor are int type since you use int() method to convert the value from raw_input() into int type.

As a result, divident / divisor is also an int type. You need to convert int to float (for example: float()) before division.

"""Given division of two numbers, the result will print out """
try:
    divident = int(raw_input("Enter the divident: "))
    divisor = int(raw_input("Enter the divisor: "))
    print (" %d devided by %d is %f: " % ( divident, divisor, float(divident) / float(divisor)))

except(ValueError, ZeroDivisionError):
    print ("Something went wrong!")

Output:

Enter the divident: 12
Enter the divisor: 5
 12 devided by 5 is 2.400000: 

Note, if your inputs are not integers, consider converting them to float at the begining:

divident = float(raw_input("Enter the divident: "))
divisor = float(raw_input("Enter the divisor: "))
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23
0

You must declare the input type as float in place of int because the input type determines the output type. You should try:

a=float(input('your prompt string'))
b=float(input('your 2nd prompt'))
print(a/b)