错误代码试图加载个别的学生级菜单,所有学生

大家好,所以我创建一个学生成绩的菜单,我想进入一个学生的名字,并使用咸菜2个测验成绩。一旦输入这些成绩,我想创建一个报告,并且在我尝试为我的菜单选项2搜索单个学生并向菜单选项三显示所有学生时,但每次尝试打印时都会收到错误代码它。错误代码试图加载个别的学生级菜单,所有学生

import pickle 

def menu():

selection = input("0\tExit"

"\n1\tEnter Student Name/Grades"

"\n2\tIndividual Report"

"\n3\tReports"

"\nEnter Menu Number: ")

if selection == "0":

systemExit()

if selection == "1":

studentData()

if selection == "2":

singleReport()

if selection == "3":

studentReports()

def systemExit():

exit()

def studentData():

name = input("Enter Student Name: ")

quiz1 = input("Enter Quiz 1: ")

quiz2 = input("Enter Quiz 2: ")

with open("pStudent_Quiz_Grades.p", "ab") as pFile:

pickle.dump((name, (quiz1, quiz2)), pFile)

clearScreen()

return()

def clearScreen():

print("\n" * 5)

return()

def singleReport():

pFile = open("pStudent_Quiz_Grades.p", "rb")

grades_dict = pickle.load(pFile)

search = input("Enter a Name to Search: ")

for name in grades_dict:

if name.upper() == search.upper():

print(name+": "+str("pStudent_Quiz_Grades.p"[name]))

def studentReports():

pFile = pickle.load(open("pStudent_Quiz_Grades.p", "rb"))

print(pFile)

while True:

menu()

回答:

我立足我的答案只有我已经被赋予了错误,读作:

Traceback (most recent call last): File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 57, in <module> menu() File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 21, in menu singleReport() File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 49, in singleReport if name.upper() == search.upper(): AttributeError: 'tuple' object has no attribute 'upper' 

的错误,指出的名字是一个元组,其不能有方法上,只有串允许此方法。所以我建议使用打印做一个简单的调试,你可以这样做:

for name in grades_dict: 

print "name", name "and type is", type(name)

if name.upper() == search.upper():

print(name+": "+str("pStudent_Quiz_Grades.p"[name]))

与此打印语句,您可以检查该名称是什么样子,以及数据名称的类型是很明显应该是个元组。另外,如果我们假设搜索是一个字符串变量,那么您不能将字符串与元组进行比较。所以我建议在你做完调试之后,就是将名称转换为字符串,这样你可以让程序比较搜索是否是字符串。

以上是 错误代码试图加载个别的学生级菜单,所有学生 的全部内容, 来源链接: utcz.com/qa/259956.html

回到顶部