Python 字典打印不正确
字典输入不正确或打印过程不成功,不确定修复哪一个和如何修复Python 字典打印不正确,python,dictionary,Python,Dictionary,字典输入不正确或打印过程不成功,不确定修复哪一个和如何修复 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 clas
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)]
>>>
我试着运行你的代码,但它没有打印任何东西。请提供一个。最后一行应该是
sortedavgapha
,而不是sortedavgalpha
。(您错过了一个l
)并且ClassCall()
一直在调用自己(就在except之后)