字典输入无法正常工作或打印程序无法正常工作,不确定哪个以及如何修复。
def ClassCall():
global classes
global Classdict
global Classopen
global students
global people
try:
classes = input("Which class would you like to view? ")
if classes == 1 or classes == 2 or classes == 3:
print "Thank you"
Classopen = open("Student" + "class" + str(classes) + ".csv","rb")
Classfile = csv.reader(Classopen)
Classdict = {}
for people in Classfile:
students = people[0]
studentvalue = str(people[1])
if students in Classdict:
Classdict[students].append(studentvalue)
else :
Classdict[students] = [studentvalue]
else:
print "Enter a valid calss"
ClassCall()
except:
print "This class is currently not added"
ClassCall()
ClassCall()
global x
x = 1
while x == 1:
Question = raw_input("""How would you like to view the class?
A) By Average, highest to lowest:
B) By Highest score, highest to lowest:
C) By Alphaetical order:
""")
Question = Question.title()
if Question == "A" :
for key, value in Classdict.items():
Avg = sum(map(int, value))/ float(len(value))
global AvgDict
AvgDict = {}
students = people[0]
if key in AvgDict:
AvgDict[key].append[Avg]
else:
AvgDict[key] = Avg
print AvgDict
Classopen.close()
x = x + 1
qtwo()
def qtwo():
Questiontwo = raw_input("""How would you like to view it?
A)By highest score, highest to lowest:
B)By Alphaetical order:
""")
for key,value in Classdict.items():
Questiontwo = Questiontwo.title()
if Questiontwo == "A":
print "You selected highest score, highest to lowest"
sortedavghigh = sorted(AvgDict.items(), key=operator.itemgetter(1))
print sortedavghigh[::-1]
elif Questiontwo == "B":
print "You selected to sort it alphabetically"
sortedavgapha = sorted(AvgDict.items(), key=operator.itemgetter(0))
print sortedavgalpha
此代码产生此代码并且不会打印整个平均字典,但是如果我将其打印在平均代码旁边则可以正常工作。但是我不能这样做,因为它需要再次排序
You selected highest score, highest to lowest
[('tom', 3.0)]
You selected highest score, highest to lowest
[('tom', 3.0)]
You selected highest score, highest to lowest
[('tom', 3.0)]
>>>