0

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!

Danyal Rana
  • 33
  • 1
  • 4

3 Answers3

2

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.

Community
  • 1
  • 1
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

You should use None instead of undefined.

if done == 0 or done is None:
    print(Good job!)
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
0

The None value is as close to an 'undefined' as you can get. Set the initial value of done to be None then you can test for:

if done == 0 or done is None
zenlc2000
  • 451
  • 4
  • 9