大家好,所以我正在创建一个学生成绩菜单,我想输入学生姓名和两个使用泡菜的测验成绩。输入这些成绩后,我想创建一个报告,在我尝试为我的菜单选项2搜索单个学生后,向所有学生显示菜单选项3,但每次我尝试打印时都会收到错误代码它。
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()
答案 0 :(得分:0)
我的答案仅基于我给出的错误,其内容如下:
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'
该错误声明该名称是一个元组,它不能使用方法上限而且只有字符串允许此方法。因此,我建议使用print进行简单的调试,您可以执行以下操作:
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]))
使用此打印语句,您可以检查名称的外观以及数据名称的类型,显然应该是一个元组。此外,如果我们假设搜索是字符串变量,则无法将字符串与元组进行比较。所以我建议你在调试之后将名称转换为字符串,这样你就可以让程序比较搜索是否为字符串。