python return - “name not defined”

后端 未结 3 1123
青春惊慌失措
青春惊慌失措 2021-01-27 07:12

I\'m trying to get this script to write information into a basic text file. I\'m still learning python.

The problem lies here:

file_text = \"\"\"%s SHOOT         


        
3条回答
  •  暖寄归人
    2021-01-27 07:39

    The variables only exist in the scope you declare them

    def fun():
        x = 5
        return x
    
    >>> x
    Traceback (most recent call last):
      File "", line 1, in 
        x
    NameError: name 'x' is not defined
    

    If you want to use the return value from a function, then call the function and assign it to such a variable

    >>> x = fun()
    >>> x
    5
    

提交回复
热议问题