+ 1
Hey I usually test my codes on phone on Dcoder .I got 4/2=2 in only python and 4/2= 2.0 in python3. why?
13 ответов
+ 3
Using int built in function.
x=4
print(int(x/2)) #output=2
+ 2
Python 2.x assumes the integer result of division as float only if either of operands is float:
4/2 = 2, but 4/2.0 = 2.0 and 4.0/2 = 2.0
Python 3 assumes the result of the division as float no matter if it is integer or not:
4/2 = 2.0
+ 2
I think Dcoder is using python 2.x.
in python3 it will give you output in float.
+ 2
partha the reason behind it because a/2 create a float number in python3.x and float values can't multiple with string.
but in python2.x a/2 is create a int and int can multiple with string.
+ 2
I'm right Dcoder is on python2.x
+ 2
check this post partha meaning of //
https://www.sololearn.com/discuss/1463470/?ref=app
+ 1
partha , Maninder Singh is right, you need to convert division result to int. This will work in both python branches. // is a floor division, it returns the closest lower integer to the result.
0
hey strawdog and Maninder Singh it's this code
a=4
print("hi"*(a/2))
I even tried in Dcoder it's working fine in 'python' but showing error in 'python3'. it's also showing error in sololearn playground
0
Maninder Singh how to get the same result in python 3?
Dcoder has both
0
hey Maninder Singh I just got that it can be done this way too
a=4
print("hi"*(a//2))
and it worked but can't figure out how(what's "//" for?)
though using it in "python" changes nothing
0
thanks Maninder Singh andstrawdog ✌️✌️