I am trying to access a global variable in a function however it seems to believe it to be NoneType. Any help? The error seems to believed that the dictionary 'students' is NoneType and it is not reading it as a global variable even though I tell it to. Is this an error with my code or is it an issue with this version of python, I cannot replicate the error on any other program (using versions from 2.7-3.3.3!)
#Imports
from math import floor
#Global Declarations (for later)
#Functions
def loadData():
"""Loads the students.txt file if it exists."""
studentsFile = open("students.txt", "r")
data = studentsFile.read()
students = exec(data)
studentsFile.close()
return students
def fileExists():
"""Checks if the students.txt file exists."""
try:
studentsFile = open("students.txt", "r")
studentsFile.close()
students = loadData()
except FileNotFoundError:
studentsFile = open("students.txt", "w")
studentsFile.close()
students = {}
print(students)
return students
def saveToFile():
global students
studentsFile = open("students.txt", "w")
studentsFile.write(str(students))
studentsFile.close()
def gradeCalc(score):
possibleGrades = {"A*" : [x for x in range(90, 101)], "A" : [x for x in range(80, 90)], "B" : [x for x in range(70, 80)], "C" : [x for x in range(60, 70)], "D" : [x for x in range(50, 60)], "E" : [x for x in range(40, 50)], "F" : [x for x in range(30, 40)], "G" : [x for x in range(20, 30)], "U" : [x for x in range(0, 20)]}
for key in possibleGrades:
if int(floor(score)) in possibleGrades[key]:
grade = key
def studentExists(studentName):
global students
print(students)
print(studentName)
try:
testVar = students[studentName]
return True
except KeyError:
return False
def doneTest(studentName, testName):
try:
testVar = students[studentName][testName]
return True
except KeyError:
return False
def integerInput(msg):
while True:
try:
integer = int(input(msg))
return integer
except ValueError:
print("Not a whole number.")
def scoreInput():
while True:
studentScore = integerInput("Enter the students score: ")
if studentScore <= 100 and studentScore >= 0:
break
else:
print("Not between (and including) 0 and 100.")
return studentScore
def getData():
global students
quantity = integerInput("How many students do you wish to add? ")
test = str(input("Please enter a test name: ").lower())
for i in range(0, quantity):
name = str(input("Student name: ").title())
score = scoreInput()
if studentExists(name) == False:
students[name] = {}
students[name][test] = score
students[name][test+"Grade"]=gradeCalc(score)
def displayData():
global students
studentsToDisplay = []
listOfGrades = []
print("You must choose a mode. How many students got each grade or the grades for an individual student.")
mode = input("Please select 'grade' or 'student'").lower()
if mode == 'grade':
test = str(input("For which test do you want to access the grades for? ").lower())
for key in students:
if doneTest(key, test)==True:
studentsToDisplay.append(key)
for item in studentsToDisplay:
listOfGrades.append(students[item][test+"Grade"])
print("Grades:\nA*:", ListOfGrades.count("A*"), "\nA:", ListOfGrades.count("A"), "\nB:", ListOfGrades.count("B"), "\nC:", ListOfGrades.count("C"), "\nD:", ListOfGrades.count("D"), "\nE:", ListOfGrades.count("E"), "\nF:", ListOfGrades.count("F"), "\nG:", ListOfGrades.count("G"), "\nU:", ListOfGrades.count("U"))
elif mode == "student":
studentName = str(input("Student name: ").title())
test = str(input("Test name: ").lower())
if doneTest(studentName, test) == True:
print(studentName, "got", students[studentName][test], "and this is grade:", students[studentName][test+"Grade"])
def main():
global students
students = fileExists()
while True:
choice = str(input("Do you want to add students/tests or do you want to review previous data? (accepts 'add' and 'review') ").lower())
if choice == "add":
getData()
saveToFile()
elif choice == "review":
displayData()
else:
exitChoice = str(input("Do you want to exit? ").lower())
if exitChoice == "yes" or exitChoice == "y":
break
main()
I get this error:
Traceback (most recent call last):
File "V:\GCSE 2013\sj.sully\Programme Library\Assessed Tasks and CAT Practice\CAT Practice Scenario 1 Code.py", line 108, in <module>
main()
File "V:\GCSE 2013\sj.sully\Programme Library\Assessed Tasks and CAT Practice\CAT Practice Scenario 1 Code.py", line 100, in main
getData()
File "V:\GCSE 2013\sj.sully\Programme Library\Assessed Tasks and CAT Practice\CAT Practice Scenario 1 Code.py", line 71, in getData
if studentExists(name) == False:
File "V:\GCSE 2013\sj.sully\Programme Library\Assessed Tasks and CAT Practice\CAT Practice Scenario 1 Code.py", line 39, in studentExists
testVar = students[studentName]
TypeError: 'NoneType' object is not subscriptable
I am using python 3.3.0.