Python Forum
Def code does not work for negative integers.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Def code does not work for negative integers.
#1
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.
Reply
#2
I would test against the absolute value of x (abs(x)) instead of just x.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cmath.rect accepts a negative modulus JMB 2 303 Jan-17-2024, 08:00 PM
Last Post: JMB
  hi need help to make this code work correctly atulkul1985 5 698 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 763 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,679 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,380 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 838 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,128 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,311 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  How to do bar graph with positive and negative values different colors? Mark17 1 5,000 Jun-10-2022, 07:38 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020