Python Forum

Full Version: Def code does not work for negative integers.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
I would test against the absolute value of x (abs(x)) instead of just x.