兩個不同定義之間的變量 (Variable between two different definitions)


問題描述

兩個不同定義之間的變量 (Variable between two different definitions)

我想從 b() 調用 var d。但我得到這個錯誤。我聽說你可以有全局變量,我試過但沒有成功。

錯誤:

Traceback (most recent call last):
  File "C:/Users/user2/Desktop/def.py", line 9, in <module>
    a()
  File "C:/Users/user2/Desktop/def.py", line 3, in a
    if d == 0:
NameError: name 'd' is not defined

代碼:

def a():
    if d == 0:
        print(correct)
    else:
        print (not correct)
def b():
    d = 0

a()

參考解法

方法 1:

You can define the variable outside the function and it should work. Although it is better to pass as argument.

d=0
correct="It is correct"
notcorrect="It is not correct"

def a():
    if d == 0:
        print(correct)
    else:
        print(notcorrect)

a()

方法 2:

You can use variables of a "parent" scope, even if it's better to pass them to the method. Your function b() is never called in your example. And definitions in a function are just defined for this functions or functions called from there.

I would recommend you to read about scopes: https://pythonspot.com/scope/ (there are tons of other tutorials out there, just use your search engine ;))

what you could do:

d = 0
a()  # correct

what you could do as well:

def b():
   d = 0
   a()

b()  # correct

but what you SHOULD do is probably something like:

def b():
  d = 0
  return d

def a(d):
  ...

a(b())  # correct

global variables exist in python, but especially for beginners it often seems to be an easy solution, but as soon as your code grows this can become very complex if not used carefully.. understanding scopes of variables and how to pass them into other functions is the way to go.

方法 3:

The code will definitely show an error because the variables created within the function are for that function only called local variables, and the variable created outside the function are called global variables.

hence,

  1. you simply have to create the variable outside the function globally then it will not shows error.
def a(d):
    if d == 0:
        print("correct")
    else:
        print ("not correct")
#################
a(0)
  1. or else you can use inbuilt function global() if you want to use the variable inside the different functions. and make sure you have the functions as well.

(by Benjamin DixonDavid DuranLukrRohit)

參考文件

  1. Variable between two different definitions (CC BY‑SA 2.5/3.0/4.0)

#Variables #Python






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論