Ask coding questions

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

I'm trying to create code that checks a number's truthiness.
Because:
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!!!
https://repl.it/@JerryTheMouse/numberTruthynessChecker

Commentshotnewtop
madmonster (7)

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

ash15khng (234)

@JerryTheMouse A bit late, but why not just do:
print(bool(float(input("Enter number you want to check: "))))
Explanation:
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 (3)

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 (3)

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

ash15khng (234)

@JerryTheMouse bool actually does work on numbers

JerryTheMouse (3)

Thanks for that new tip, @ash15khng!!!

theangryepicbanana (687)

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 (144)

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:
     run_this_code()
else:
    run_that_code()

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 (940)

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

JerryTheMouse (3)

@timmy_i_chen because it only limits to only those 2 nums