jeudi 26 mars 2015

Python - Sorting file highest to lowest and alphabetically

I have created an arithmetic code, that askes the user 10 questions, and then stores there name and score in a data base e.g "C:\class1.txt" and am now at the stage, where I should be able to sort the file containing the name and the score of multiple pupils from each individual class, in both highest to lowest (scores) and alphabetically. The programme should ask a question at the end of the code, asking the teacher if they want it sorted alphabetically or highest to lowest by score. They should also be able to pick the class the want sorted, and it should be printed.


I am asking for guidance on this, I do not want to cheat, I am just now clueless at this stage; with a teacher that is useless.


Thanks in advance



import random
USER_SCORE = 0
questions = 0
classnumber = ("1","2","3")
name1= input("Enter Your Username: ")
print("Hello, " + name1)
print(" Welcome to the Arithmetic Quiz")

classno = input("What class are you in?")
while classno not in classnumber:
print("Enter a valid class")
print("ENTER ONLY THE NUMBER n\ 1 n\ 2 n\3")
classno=input("What class are you in?")

while questions <10:
for i in range(10):
num1=random.randint(1,10)
num2=random.randint(1,10)
on=random.choice("*-+")
multiply=num1*num2
subtract=num1-num2
addition=num1+num2

if on == "-":#If "-" or subtract is randomly picked.
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question" ,questions, "/10")
uinput=input(str(num1)+" - "+str(num2))
if uinput == str(subtract):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: " ,USER_SCORE,)
else:
print (" Incorrect, the answer is: " +str(subtract))
USER_SCORE+=0

if on == "+":
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question",questions, "/10")
uinput=input(str(num1)+" + "+str(num2))
if uinput == str(addition):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: ",USER_SCORE,)
else:
print(" Incorrect, the answer is: " +str(addition))
USER_SCORE+=0

if on == "*":
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question",questions, "/10")
uinput=input(str(num1)+" * "+str(num2))
if uinput == str(multiply):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: " ,USER_SCORE,)
else:
print(" Incorrect, the answer is: " +str(multiply))
USER_SCORE+=0

if USER_SCORE >9:
print("Well done," ,name1, "your score is" ,USER_SCORE, "/10")
else:
print(name1," your score is " ,USER_SCORE, "/10")

def no1():
with open("no1.txt", 'a')as file:
file.write(str(name1)+" achieved a score of: "+str(USER_SCORE)+"/10 \n")
def no2():
with open("no2.txt", 'a')as file:
file.write(str(name1)+" achieved a score of "+str(USER_SCORE)+"/10 \n")
def no3():
with open("no3.txt", 'a')as file:
file.write(str(name1)+" achieved a score of "+str(USER_SCORE)+"/10 \n")
if classno=="1":
no1()
if classno=="2":
no2()
if classno=="3":
no3()

Aucun commentaire:

Enregistrer un commentaire