Danyal Rana Danyal Rana - 10 months ago 67
Python Question

Pyscripter: How do I use if statements with an undefined variable?

This is the part of my code i need help with:

if done==0 or done== undefined:
print("Good job!")

I made another "If" statement earlier where I give "done" a value, but if it was not given a value, (there is no input) and i want the code to be "if 'done' is 0, or 'done' is undefined" do what ever, for example print "Good job!" if it 'done' 0 or undefined

Is there a way I could do this? Any help would be very much appreciated!

My question has been solved. Thank you to everyone who answered!

Answer Source

The best, most pythonic way is to initialize done at the very beginning to something that is clearly meaningless. Then you can test for that meaninglessness. The pythonic way is

done = None

where None is a special value that means nothing meaningful. In other words, that is (or can be) Python for "undefined." After that is done, you may assign done to zero in some circumstances. You would then later test

if done == 0 or done is None:
    print("Good job!")

For more about None, see this page.

If you really, really must leave done completely undefined and then test for that, you can use an exception. But I don't recommend it.