Python: Why is my program running when I insert pieces one by one, but not when all together?
Question: So I created this very easy paber-rock-scissors game in Python (Sorry, the
strings are not in English).

Anyway, I am running it in IDLE and it works when I insert it like this:

  1. from random import randint --> ENTER 
  2. Def my function --> ENTER
  3. call my function --> ENTER

This way it runs as expected.

But when I put them all together and then press ENTER, it does nothing and
when I call my function again, it says it's not defined. Also, when I save it
as .py and run it, it only pop's up for like 0.5 seconds.

My code is:

    
    
    from random import randint
    def myProgram():
        kpk = raw_input("Kivi, paber või kaarid?")
        random = randint(0,2)
        result = ""
        if kpk == "kivi":
            kpk = 0
            if random == 0:
                result = "VIIK"
            elif random == 1:
                result = "Kivi < Paber --- KAOTASID"
            else:
                result = "kivi > kaarid --- VOITSID"
        elif kpk == "paber":
            kpk = 1
            if random == 0:
                result = "kivi < paber --- VOITSID"
            elif random == 1:
                result = "VIIK"
            else:
                result = "paber < kaarid --- KAOTASID"
        elif kpk == "käärid":
            kpk = 2
            if random == 0:
                result = "kivi > kaarid --- KAOTASID"
            elif random == 1:
                result = "paber > kaarid --- VOITSID"
            else:
                result = "VIIK"
        else:
            print "Sisend peab olema uks kolmest: Kivi, paber või kaarid"
            myProgram()
        print result
        kas = raw_input("Kas soovite uuesti mangida? (Y/N)")
        if kas == "Y" or kas == "y" or kas == "jah":
            myProgram()
        else:
            print "Mang on läbi!"
    myProgram()
    


Answer: I am guessing this is the minimal version of your issue -

    
    
    >>> def blah():
    ...     return 1
    ... blah()
      File "<stdin>", line 3
        blah()
           ^
    SyntaxError: invalid syntax
    >>> blah()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'blah' is not defined
    

In the python shell/IDLE , you need to enter a newline (empty line) after a
block has ended, so that python can interpret the block (this is valid for any
blocks, like function definition, loops, `with` block, etc etc).

In the above, when I copied and pasted the code-

    
    
    def blah():
        return 1
    blah()
    

Without spaces, it would give the syntax errors , as there is not newline
after the definition of function. One way (as you already observed) is to copy
paste the function and function call separately (and make sure function gets
defined by pressing enter/adding a newline at end) . (I am not sure if this is
some kind of bug in the python shell, but it has been like that for both
Python 2.x as well as Python 3.x ) .

Please note this is only when copy pasting code to the shell, not when saving
your script as a .py file and running it , when running the code as a script,
then newlines are not needed (though it would be better to put them for
readability) .

