Python 无法在简单成绩程序中访问全局变量

Python unable to access global variable in simple grade program

我正在尝试创建一个 python 程序,但我在访问函数中的全局变量时遇到了问题。请检查下面的代码并向我建议程序有什么问题。

我正在使用的所有全局变量都没有被最后一个函数访问showGrades()

我收到以下错误: NameError: name 'marks' is not defined

def main():
  getInput()
  getGrades()
  showGrades()
def getInput():
    global Quiz,Ass,Test1,Test2,marks,Grade,GradePoint
    Quiz = float(input("Enter quiz overall marks out of 100 :"))
    Ass =  float(input("Enter assignment overall mark out of 100 :"))
    Test1 = float(input("Enter test1 overall mark out of 100 :"))
    Test2 = float(input("Enter test2 overall mark out of 100 :"))
def getGrades():
    marks = float((Quiz * 0.2) + (Ass * 0.2) + (Test1 * 0.3) + (Test2 * 0.3))
    if marks<=100 and marks>=94:
     Grade = "A+"
     GradePoint = 4.0
    elif marks<=93 and marks>=87:
      Grade = "A"
      GradePoint =3.7
    elif marks <= 86 and marks >= 80:
      Grade = "A-"
      GradePoint = 3.5
    elif marks <= 79 and marks >= 77:
     Grade = "B+"
     GradePoint = 3.2
    elif marks <= 76 and marks >= 73:
     Grade = "B"
     GradePoint = 3.0
    elif marks <= 72 and marks >= 70:
     Grade = "B-"
     GradePoint = 2.7
    elif marks <= 69 and marks >= 67:
     Grade = "C+"
     GradePoint = 2.3
    elif marks <= 66 and marks >= 63:
     Grade = "C"
     GradePoint =2.0
    elif marks <= 62 and marks >= 60:
     Grade = "C-"
     GradePoint = 1.7
    elif marks <= 59 and marks >= 50:
     Grade = "D"
     GradePoint =1.0
    else:
     Grade = "F"
     GradePoint = 0.0
def showGrades():
     print(f"your overall marks are:" , float(marks))
     print("Grade:" + str(Grade))
     print(f"GradePoint:" ,float(GradePoint))
main()

getInput 函数中,您声明 marks 将是一个全局变量,但从未实际分配给它。由于 global 关键字的范围仅在该函数内,您实际上从未创建全局变量 marks.

然后在 getGrades 中创建另一个变量 marks,它是 getGrades 的局部变量,因为您没有在 getGrades 中将 marks 声明为全局变量=].这就是为什么showGrades找不到变量marks的原因;它在 getGrades.

本地

尝试将 marks 声明为 getGrades 的全局内部,我很确定问题会得到解决。

更多请看:What are the rules for local and global variables in Python?


编辑:为了证实我的想法,我决定做一个实验:我创建了一个声明但从未分配全局变量的函数。然后我们 运行 函数,并使用内置的 globals 函数来检查 myglobalvariable 是否确实存在于全局范围内

>>> def globaltest():
       global myglobalvariable

>>> globaltest()
>>> globals()

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'globaltest': <function globaltest at 0x0000027E24B11EA0>}

我们从上面的输出中看到,正如预期的那样,myglobalvariable 不在全局范围内。

在函数getGrades()里面,我会建议你放置

global mark, Grade, GradePoint

在第一行。那么问题应该就解决了。

在 Python 中,“globals”声明实际上并没有使变量成为全局变量。

您必须在正确的“全局”范围内声明一个变量,然后使用“全局”告诉函数在全局范围内有一个变量并使用它而不是在本地创建一个。

因此,在调用 main() 之前,请在同一缩进中声明所有全局变量:

Quiz=None
Ass=None
Test1=None
Test2=None
marks=None
Grade=None
GradePoint=None

main()

然后在showGrades()中,告诉它变量存在于全局范围内:

def showGrades():
     global marks, Grade, GradePoint

     print(f"your overall marks are:" , float(marks))
     print("Grade:" + str(Grade))
     print(f"GradePoint:" ,float(GradePoint))

您需要告诉函数 getGrades() 使用它所使用的全局变量。