Print function not working

Michael Selik michael.selik at gmail.com
Thu Aug 11 13:54:31 EDT 2016


On Thu, Aug 11, 2016 at 1:38 PM MRAB <python at mrabarnett.plus.com> wrote:

> On 2016-08-11 18:18, Chris Angelico wrote:
> > On Fri, Aug 12, 2016 at 3:03 AM, Atri Mahapatra
> > <atri.mahapatra at gmail.com> wrote:
> >> I have installed IDLE 3.5.1 and wrote the following  to check if print
> is working. When it runs, I do not see anything is printed:
> >>
> >> class Base: #{
> >>     def __init__( self ): #{
> >>         print("Hello, world: \n\n");
> >>
> >>         #}
> >>
> >>     #}
> >>
> >>
> >> if ( __name__ == " __main__"): #{
> >>     root = Base();
> >> #}
> >>
> >> Can anyone please point out the reason?
> >>
> >> Thanks,
> >> Atri
> >
> > Is name equal to main? That is, are you running this code as a top-level
> script?
> >
> It's not checking whether the name is "__main__", but whether it's "
> __main__" (note the space after the first quote).
>
> > Also: You do not need to write Python code as if it were C or Java.
> > All that extra punctuation is just putting unnecessary stress on the
> > world's punctuation mines, which are already overworked. :)
> >
> +1
>

When debugging something like this, one technique is to remove bits of code
to reduce the complexity. Remove something, run the code, check the
results, then repeat if it's still failing.

If you're not sure how class initializers work, maybe convert to a regular
function and try again.

    def foo():
        print('hello')
    if __name__ == '__main__':
        foo()

If that still doesn't work, maybe get rid of the function.

    if __name__ == '__main__':
        print('hello')

And if that still doesn't work, maybe get rid of the if-statement.

    print('hello')

I think you'll find that the single line program "print('hello')" works
just fine.



More information about the Python-list mailing list