How Does Code Recognize Positive And Negative Numbers

Question:
I want to be able to have my code see if it is a positive or a negative number and if it’s a positive number I want it to print something but then if it’s a negative number I want it to print something else. Any ideas?
Repl link:
https://replit.com/@Chandler0Bing/Watching-Your-Calories#main.py

code snippet

Screenshot 2023-03-03 8.50.36 AM

1 Like

Check if it’s greater than or less than 0

myint = -1


if myint < 0:
	print("Negative")
else:
	print("Positive")


3 Likes

Do you mean something like

if number > 0:
  print("something")
elif number == 0:
  print("something")
else:
  print("something")

I am hating my iPad capitalizing everything

2 Likes

0. is actually positive because small positive numbers round down to it, while small negative numbers (like -1e-999) round down to -0., which is useful e.g. when rounding celsius temperatures so you know whether it’s below freezing.

import math

print(
    "negative" if math.copysign(1, float(input("Enter a number"))) > 0 else "positive"
)
1 Like

Looks like your post has been solved… But I still must scream at you.

statement*
It’s*
their*

And you’re missing commas before and/or after saying their name.

2 Likes

Not always true and 0 is 0 …

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.