import sys
import random
name=input("What is your name?")
print ("Alright",name,"welcome to your maths quiz")
score=0
level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                         "Press 1 for low, 2 for intermediate "
                         "or 3 for high\n")))

if level_of_difficulty != 1  or 2 or 3:
 sys.exit()

if level_of_difficulty == 3:
    ops = ['+', '-', '*', '/']
else:
    ops = ['+', '-', '*']

for question_num in range(1, 11):
    if level_of_difficulty == 1:
        number_1 = random.randrange(1, 10)
        number_2 = random.randrange(1, 10)
    else:
        number_1 = random.randrange(1, 20)
        number_2 = random.randrange(1, 20)

    operation = random.choice(ops)
    maths = round(eval(str(number_1) + operation + str(number_2)),5)
    print('\nQuestion number: {}'.format(question_num))
    print ("The question is",number_1,operation,number_2)

    answer = float(input("What is your answer: "))
    if answer == maths:
    print("Correct")
    score = score + 1
    else:
         print ("Incorrect. The actual answer is",maths)

if score >5:
    print("Well done you scored",score,"out of 10")
else:
    print("Unfortunately you only scored",score,"out of 10. Better luck next     time")

C'est le code avec lequel je travaille, mais maintenant je veux ajouter une nouvelle variable appelée numéro de classe et enregistrer les scores des élèves dans leur classe. Si un nouvel élève prend le test, son score doit également y être enregistré et il doit pouvoir consulter les scores des étudiants précédents. J'ai essayé de le faire en écrivant ceci, mais je ne sais pas comment utiliser la fonction Shelve ou si je dois l'utiliser du tout. Je sais qu'il y a déjà des réponses à des questions similaires, mais je ne les ai pas comprises, aidez-moi et voici ma tentative de commencer, mais je ne sais pas comment continuer.

            import sys
            import random
            import shelve
            score=0
            name=input("What is your name?")
            print ("Alright",name,"welcome to your maths quiz")
            class_number = int(input("Before you start, are you in class 1, 2 or 3?"))# This line doesn't work
            if class_number != 1  or 2 or 3:
                sys.exit()
            if class_number == 1:
                open(class_1_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")


            if class_number == 2:
                open(class_2_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")

            if class_number == 3:
                open(class_3_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")
-2
John knight 22 juil. 2015 à 14:47

3 réponses

Meilleure réponse

Vous pouvez essayer les dictionnaires.

class_x_scores = {}
class_x_scores[name] = score 

À la fin, vous pouvez enregistrer ces dict dans des fichiers:

f = open(class_x_scores,'a') # a for adding newlines to the file without cleaning information
for name in class_x_scores:
    f.write(" The score of "+name + " is "+class_x_scores[name])# any format you prefer
f.close()
0
Wang Bucca 22 juil. 2015 à 12:06

Comment enregistrer les scores?

Cela signifie simplement que vous voulez avoir un stockage de données très simple ... qui peut être soit

  1. Fichier XML
  2. Fichier JSON
  3. Ou un simple fichier texte
  4. Tout type de votre propre fichier préféré

Qu'est-ce qui vous empêche d'utiliser les fichiers ci-dessus.?

-1
pmverma 22 juil. 2015 à 11:55

Vous devez utiliser pandas:

from pandas import DataFrame as df

d = df({'Student name': ['John', 'Joe', 'Lilly'], 'Score': [5.6, 2.7, 3.4]})

d.save('filename')

d.load('filename')

print d
0
Geeocode 22 juil. 2015 à 12:12