Ask coding questions

← Back to all posts
Help/Python [Solved]
JerryTheMouse (4)

I'm trying to create code that checks a number's truthiness.
if 0:
print('run if true")
#no output
if 1 :
print ('run if true')
#output: run if true

... I want to create a program to check this and there is lot's of errors, please help!!!

madmonster (17)

Try to use the keyword bool. It works well for it.

ash15khng (694)

@JerryTheMouse A bit late, but why not just do:
print(bool(float(input("Enter number you want to check: "))))
input() gets the number as a string
float() turns the string into a float (if the number isn't a float it'll just crash)
bool() checks its truthiness
print() prints the output

JerryTheMouse (4)

Thanks, @ash15khng but I’ve got my answer, bool probably does not work on numbers and I’ve already solved it (lol the title)

But thanks anyways

JerryTheMouse (4)

But u may help me w problems WITHOUT the solved tag, @ash15khng ;)

ash15khng (694)

@JerryTheMouse bool actually does work on numbers

JerryTheMouse (4)

Thanks for that new tip, @ash15khng!!!

theangryepicbanana (1631)

Unless it's javascript, you have to convert it to a number first (because "0" and "0.0" evaluate to false in js lol)

minx28 (358)

There are a few problems: firstly, when you use input(), the result is a string, so before you process i, you should do j = int(i) and use j instead. Next, you don't use is to check for conditions: in this case, you can simply do

if j:

There are only a few things in python which evaluate to False, these include:
0 '' [] () {} None set()
Everything else is True.

I hope you found this comment helpful
Upvote for vee freebucks!

timmy_i_chen (1108)

Why not just check if i == "0" or i == "1"?

JerryTheMouse (4)

@timmy_i_chen because it only limits to only those 2 nums