0

I have to complete a task like:

Read two integers and print two lines. The first line should contain integer division, a//b. The second line should contain float division, a/b.

I've tried a code like:

a = int(raw_input())
b = int(raw_input())
print a//b
print a/b

Except this, Is there any other way to solve this kind of problem? Please help..

kouty
  • 320
  • 8
  • 17
marwari
  • 181
  • 2
  • 13

5 Answers5

3
a = int(raw_input())
b = int(raw_input())
print a/b 
print a/float(b)

note - Python 2.7
in line 3- a, b both are int so a/b always give int value answer.
if one element is float type then answer in always floating value.

  • Yes this is a nice idea, it works. I've tried to write like **print float(a/b)** but it showed me an error. – marwari Aug 06 '17 at 08:58
2

Try

a = int(raw_input())
b = int(raw_input())
print a//b
print a/float(b)
Arpit Svt
  • 1,153
  • 12
  • 20
0

Other way? Keeping your code readable is the best of things. What you can do is format the print statement (example below):

Update: here is a working example for both python2 and python 3

from __future__ import division # use this if python2

a = int(input()) # in python2 input() is sufficient
b = int(input())

intdiv = a//b  
floatdiv = a/b

print("{0}\n{1}".format(intdiv,floatdiv)) # this is the "normal" way to print things. \n = Linebreak

Or:

print("{}\n{}".format(intdiv,floatdiv)) # alternative

Or:

print("Int: {}\nFloat: {}".format(intdiv,floatdiv)) # alternative

The last prints:

Int: 0
Float: 0.6666666666666666

Docs and other examples here: https://docs.python.org/2/library/string.html#format-examples

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

Check here and Try (Python 2.7.10):

a = int(raw_input())
b = int(raw_input())
print (1.0*a/b) # float value 
print a/b # int value
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
0

Try this:

a = int(raw_input())
b = int(raw_input())
print a/b
print float(a)/b
marwari
  • 181
  • 2
  • 13