Is this what you want?
def check_negative(s):
try:
f = float(s)
if (f < 0):
return True
# Otherwise return false
return False
except ValueError:
return False
Not entirely sure if this is what you want though, maybe you should see ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000's answer
Answer from heo on Stack OverflowIs this what you want?
def check_negative(s):
try:
f = float(s)
if (f < 0):
return True
# Otherwise return false
return False
except ValueError:
return False
Not entirely sure if this is what you want though, maybe you should see ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000's answer
You can replace your is_number function with this code:
def is_number(s):
return str(s)[0] == '-' and len(str(s)) > 1
It is a function which checks whether the first character is a negative sign, and if it is, it will return true. Otherwise, the number cannot be negative, and it will then return false. However, if there is only a - sign, then it is neither and will return false. It only uses one line and is much simpler. It works for strings and floats too.
python - Having trouble with 'and' 'or' statements and negative numbers - Geographic Information Systems Stack Exchange
Handling negative number inputs from the user
How do I check to see if multiple numbers are negative at once.
Can someone help me with this problem?
This is a solution post. I had a problem and none of the solutions I found online were right for me. I eventually figured it out, and so I'm putting my solution here for future learners. Also if my solution is bad, I'll get some feedback. If you think it's obvious, then you're very clever, but no need to go to the trouble of letting me know!
I'm making an arithmetic game for my little one. So it had a line:
answer = int(input(f"What is {a} + {b}?"))
but of course he accidentally typed a letter and crashed the program. I wanted to handle this eventuality so I changed it to:
answer = input(f"What is {a} + {b}")
if answer.isnumeric():
answer = int(answer)
else:
print("That's not a number")
continue
the trouble is I also have subtraction questions and negative numbers! But "-1".isnumeric()==False !!
So I started googling: "isnumeric negative numbers" and "parsing negative numbers" and so on. The solutions I found were quite convoluted, mostly they seemed to be worrying about SQL injection and used concepts I hadn't learned yet. I wanted a solution that only used the beginner stuff I already knew. I realised that I only had to check if the first symbol is "-" and the rest is numeric. So:
if answer.isnumeric() or answer[0]=="-" and answer[1:].isnumeric():
is the solution!
EDIT: There was a typo in my solution, I meant to check if everything after the initial "-" is numeric. Otherwise an answer like "-3e" gets through. Thanks to u/Rizzityrekt28 for the catch
I would group with parens as follows
if (calculation < -0.001 or calculation > 0.001) and linkup == " ":
The current conditional seems to be interpreted as "if the condition is less than -0.001, or if condition is > 0.001 as well as linkup being blank".
If the condition is negative (e.g. calculation = -30) then Python is essentially deciding it's met the first condition and it therefore does not need to look at the subsequent conditions.
if (calculation < -0.001 or calculation > 0.001) and linkup == " ":
By explicitly grouping the calculation conditions, you mandate the linkup condition has to be considered along with either calculation condition, not just the second one.
As an alternative to grouping with parentheses, you could break the conditions down further. It's less compact (and probably less Pythonic) but I find it more readable. Just an idea.
for row in rows:
if (calculation < -0.001 or calculation > 0.001):
if linkup == " ":
row.setValue("Up_LinkA", link_id)
elif linkup2 == " ":
row.setValue("Up_LinkB", link_id)
elif linkup3 == " ":
row.setValue("Up_LinkB", link_id)
elif (calculation > -0.001 and calculation < 0.001):
if linkdown == " ":
row.setValue("Down_LinkA", link_id)
elif linkdown2 == " ":
row.setValue("Down_LinkB", link_id)
elif linkdown3 == " ":
row.setValue("Down_LinkC", link_id)
rows.updateRow(row)