#### Using strings to store scores for a python maths quiz

-按名称的高低顺序命名-按最高分数从高到低-按平均分数从高到低-如果可能的话，我想使用字符串来做到这一点，如果您可以向我展示如何做到这一点，我将不胜感激。 我正在考虑使用字符串，但我不知道您会怎么做。 我到目前为止的代码是：

``````import random
def start_quiz():
print("Welcome to my Maths quiz")
#this creates a subroutine to enter the users name
def get_name():
global name
global group

while name=="":
group=input("What class are you in-1,2 or 3?")
while group=="":
group=input("What class are you in?")
print("Hello " + name)

def questions():
global score
global questionnumber
score=0
questionnumber=0
while questionnumber<10:
questionnumber=questionnumber+1
operatorlist=['+','-','*']
num1=random.randint(1,10)
num2=random.randint(1,10)
op=random.choice(operatorlist)
if op=='-' and num2>num1:
expression="%d %s %d" % (num2,op,num1)
else:
expression="%d %s %d" % (num1,op,num2)
try:
except ValueError:
print("well done")
score=score+1
print(" ")
else:

def end_quiz():
print("The quiz is now over")
print("Well done " + name+". Your score is %d out of 10" % (score))

def savethescore():
global score
score=str(score)
if group=='1':
file=open("Scoreforclass1.txt",'a')
file.write("\n")
file.write ("Name: "+name+"\n")
file.write('Class: '+group+'\n')
file.write("Score: "+score+"/10\n")
file.close()
elif group=='2':
file=open("Scoreforclass2.txt",'a')
file.write("\n")
file.write ("Name: "+name+"\n")
file.write('Class: '+group+'\n')
file.write("Score: "+score+"/10\n")
file.close()
else:
file=open("Scoreforclass3.txt",'a')
file.write("\n")
file.write ("Name: "+name+"\n")
file.write('Class: '+group+'\n')
file.write("Score: "+score+"/10\n")
file.close()

def play_again():
again= input("would you like to play again? y for yes and n for no")
while again not in ['Y','y','N','n']:
again = input("please enter 'Y' or 'N'")
if again== 'y' or 'Y':
do_the_quiz()
else:
exit()

def do_the_quiz():
start_quiz()
get_name()
questions()
end_quiz()
savethescore()
play_again()

do_the_quiz()
``````

1 个回复

``````import re
from functools import reduce

data=""
try:
except:
pass
try:
except:
pass
try:
except:
pass
allscores_name={}
allscores_score={}
averagescore_score={}

pattern=re.compile("Name: [^\n]*?\nClass: [^\n]*?\nScore: [^\n]*?\n")
data=pattern.findall(data)
for line in data:
start=line.find("Name: ")+len("Name: ")
end=line.find("\n",start)
name=line[start:end].strip()
start=line.find("Class: ",end)+len("Class: ")
end=line.find("\n",start)
classnum=line[start:end].strip()
start=line.find("Score: ",end)+len("Score: ")
end=line.find("/10\n",start)
score=line[start:end].strip()
if(name+classnum in allscores_name):
allscores_name[name+classnum].append(int(score))
else:
allscores_name[name+classnum]=[int(score)]
if(int(score) in allscores_score):
allscores_score[int(score)].append(name+classnum)
else:
allscores_score[int(score)]=[name+classnum]

out=open("sortedbyname.txt",'w+')
for name in sorted(allscores_name.keys()):
average=reduce(lambda x, y: x + y,map(int,allscores_name[name])) / float(len(allscores_name[name]))
if(average in averagescore_score):
averagescore_score[average].append(name)
else:
averagescore_score[average]=[name]
for score in sorted(allscores_name[name]):
out.write("\n")
out.write ("Name: "+name[:-1]+"\n")
out.write('Class: '+name[-1]+'\n')
out.write("Score: "+str(score)+"/10\n")
out.close()

out=open("sortedbyscore.txt",'w+')
for score in sorted(allscores_score.keys())[::-1]:
for name in sorted(allscores_score[score]):
out.write("\n")
out.write ("Name: "+name[:-1]+"\n")
out.write('Class: '+name[-1]+'\n')
out.write("Score: "+str(score)+"/10\n")
out.close()

out=open("sortedbyaverage.txt",'w+')
for avgscore in sorted(averagescore_score.keys())[::-1]:
out.write("\n")
out.write ("Name: "+name[:-1]+"\n")
out.write('Class: '+name[-1]+'\n')
for name in sorted(averagescore_score[avgscore]):
for score in sorted(allscores_name[name])[::-1]:
out.write("Score: "+str(score)+"/10\n")
out.close()
``````

2 Python 数学测验

2015-05-05 14:11:40 5 9618   python
3 在python中修复数学测验

2016-03-21 09:49:57 2 208   python
5 Python数学测验字母排序

7 python的数学测验-str对象没有属性

8 Python数学测验 - 输出正确或不正确

2015-02-25 20:48:13 2 880   math
9 python：数学测验-数据未存储

2015-01-22 01:07:05 1 691   python