2015-07-08 2 views
0

Для моей вычислительной работы мне пришлось создать арифметическую викторину, которая сохранила бы счет пользователей в файле. Я завершил эту часть, но для следующей задачи мне нужно, чтобы имена пользователей печатались в алфавитном порядке с указанием их самых высоких результатов. Затем распечатайте самый высокий рейтинг до самого низкого (показывая, какие пользователи получили этот балл), а затем найдите средний балл каждого пользователя и распечатайте средние баллы с наивысшим до самого низкого порядка (снова показывая, какой пользователь получил тот счет). К сожалению, я не знаю, как это сделать. Пожалуйста, помогите мне. Мой код до сих пор это:Как заказать элементы в функции?

import random 
from random import randint 
import math 
import fileinput 

question = 0 
ans = 0 
score = 0 

firstname = input("What is your first name?") 
lastname = input("What is your last name?") 
classofstudent = input("Which class are you in? (1,2 or 3?)") 
print ("Welcome to this arithmetic quiz") 
print ("") 
for question in range(10): 
    def multiplication(): 
     global ans 
     numberOne, numberTwo = random.randint(0,10), random.randint(0,10) 
     print ("What is", numberOne, "*", numberTwo) 
     ans = (numberOne * numberTwo) 

    def subtraction(): 
     global ans 
     numberOne, numberTwo = random.randint(0,10), random.randint(0,10) 
     print ("What is", numberOne, "-", numberTwo) 
     ans = (numberOne - numberTwo) 

    def addition(): 
     global ans 
     numberOne, numberTwo = random.randint(0,10), random.randint(0,10) 
     print ("What is", numberOne, "+", numberTwo) 
     ans = (numberOne + numberTwo) 

    operation = [multiplication, subtraction, addition] 
    randoperation = random.choice(operation) 
print() 
def main(): 
    question = 0 
    score = 0 
    randoperation = random.choice(operation) 

    whileTrue: 
     try: 
      randoperation() 
      randoperation = random.choice(operation) 
      if question >= 10: 
       break 

      userinput = int("What is your answer?") 
      if userinput = ans: 
       global score 
       score = score + 1 
       global question 
       question += 1 
       print ("Your answer is correct") 
      else: 
       print ("Incorrect") 
       question += 1 
     except ValueError: 
      print ("Invalid answer") 
      question += 1 
     while question == 10: 
      print (firstname ,lastname ,"you scored" , score,"/10") 
      break 
def filewrite(): 
    fileopen = open("data.txt", "a") 
    counts = str(score) 
    cos = str(classofstudent) 
    Textline = (firstname + " " + lastname + "/class " + cos + " /score = " + counts + "/10" + "\n") 
    fileopen.write(Textline) 
    fileopen.close 

def read(): 
    with open ('data.txt') as data: 
     print ("") 
     for line in data: 
      print (line, end ='') 
     print ("") 
    check = "Y" 

main() 
filewrite() 
read() 

ответ

0

Вы могли бы начать читать ваши результаты в виде списка или словаря и использовать функцию sorted() заказать элементы в алфавитном, по возрастанию или убыванию заказов.

>>> elements = [22, 333, 0, -22, 1000] 
>>> print sorted(elements) 
[-22, 0, 22, 333, 1000] 
>>> 
>>> 
>>> for element in reversed(sorted(elements)): 
...  print element 
... 
1000 
333 
22 
0 
-22 

Отъезд этот учебник о Sorting.

Смежные вопросы