Ask coding questions

← Back to all posts
1
Help/Python [Solved]

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
1

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

1

@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

1

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

1

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

1

@JerryTheMouse bool actually does work on numbers

1

Thanks for that new tip, @ash15khng!!!

1
1

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

1

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!

1

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

1

@timmy_i_chen because it only limits to only those 2 nums