BH1603
Badges: 4
Rep:
?
#1
Report Thread starter 3 years ago
#1
Hi, I have started on Task 2 of my Maths Quiz. When I looked at the scenario, I thought it would look a little bit simple to do - all it was to do is file read and write on the information of the quiz.

Here is the scenario for task 2:
"The teacher wants to keep tack of the scores each member of the class obtains in the quiz. There are three classes in the school and the data should be kept separately for each class.

Analyse the requirements in detail for this program and design, code, test and evaluate a program will record and store the data for three separate classes of students using the arithmetic quiz."

For what I am doing is adding a bit of code that will make a text file at the end of the quiz. So, when a user has finished the quiz, the score will be recorded in to a text file with the name. The text file must be named with a class number.

Here is my code, but doesn't work:
filename = class_name + ".txt"
file = open(class_name , 'a'
file.write(name + "with ",str(correct_questions) + "out of 10."
file.write('\n'
file.close()

Much help would be needed as appreciated. My deadline for this is next week, so I need a quick response. Apart from that, I will carry on the research and I will give you a solution if I managed to get it to work for my quiz.

Many thanks,

BH1603
0
reply
BH1603
Badges: 4
Rep:
?
#2
Report Thread starter 3 years ago
#2
I have figured out that this bit of program does work, but I want it so that the information gets to be put on automatically instead of me typing it in the program manually.

The program:
with open("ClassScores.txt", "a") as my_file:
username = 'Ben' # I want this to be someones name as someone inputs their name in before the quiz starts.
class_name = 3 # Want the input from the user, not from the program.
correct_answers = 10 # When the quiz finishes, the file should update when the results have been resulted.

my_file.write("{} in class {} with {} out of 10.\n".format(username,class_nam e,correct_answers)) # This is okay, no need to change anything here.
0
reply
CyberJake
Badges: 11
Rep:
?
#3
Report 3 years ago
#3
(Original post by BH1603)
I have figured out that this bit of program does work, but I want it so that the information gets to be put on automatically instead of me typing it in the program manually.

The program:
with open("ClassScores.txt", "a" as my_file:
username = 'Ben' # I want this to be someones name as someone inputs their name in before the quiz starts.
class_name = 3 # Want the input from the user, not from the program.
correct_answers = 10 # When the quiz finishes, the file should update when the results have been resulted.

my_file.write("{} in class {} with {} out of 10.\n".format(username,class_nam e,correct_answers)) # This is okay, no need to change anything here.

So, without helping you too much, saving a users input as a variable isn't that hard.

ex.
name = input("What is your name")
Will firstly ask the user 'What is your name?' in console and the users response will then be stored as the 'name' variable.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you worried that a cap in student numbers would affect your place at uni?

Yes (111)
59.68%
No (40)
21.51%
Not sure (35)
18.82%

Watched Threads

View All