Python Forum
Def code does not work for negative integers. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Def code does not work for negative integers. (/thread-22145.html)



Def code does not work for negative integers. - doug2019 - Oct-31-2019

The def code when it results in negative does not work. I don't know how to solve it.
def conversao(x):
    if x < GPa:
        return(str(int(x/MPa))+"MPa")
    elif x >= GPa:
        return(str(int(x/GPa))+"GPa")
GPa=1000000000
MPa=1000000
y=-2*10**9
z=y/2
print(f"{conversao(z)}")
The output should be -1GPa and not -1000MPa.


RE: Def code does not work for negative integers. - ichabod801 - Oct-31-2019

I would test against the absolute value of x (abs(x)) instead of just x.