Reputation: 191
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..
Upvotes: 0
Views: 3372
Reputation: 191
Try this:
a = int(raw_input())
b = int(raw_input())
print a/b
print float(a)/b
Upvotes: 0
Reputation: 18906
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
Upvotes: 0
Reputation: 128
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.
Upvotes: 3
Reputation: 2950
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
Upvotes: 0
Reputation: 1203
Try
a = int(raw_input())
b = int(raw_input())
print a//b
print a/float(b)
Upvotes: 2