08 February 2018

How to create a quiz using Python Programming language

How to create a quiz using Python Programming language


There is a structure to when you write code.
We first lists the steps, then we write the code.

Let us look at the structure for when you create a Quiz

Let us look at the structure of the file first.

The basic layout of the quiz
1.User input basic information and save them in file
2.User choose topic to test
3.Start test
4. Get results

Part 1
Input the basic information
Read the input information
save in the file

We can define 2 functions at this point

def read_user_info():
userInfo = input(Enter your name:")
Add some rules here
write_file(filename, content)

def write_file(filename, content):
with open(filename, "a+") as f:
f.write(content)

User choose topic to test
Start test

Here is we need Merge our topics maths() and history() computing()

The structure of those two functions:
1.open file
#maths
with open("maths.txt","r") as topic1:
...
#history
with open("history.txt","r") as topic2:
...

2.user choose difficulty level
#maths
difficultyLevel = input("Please select a difficulty \
level for the  maths quiz:easy, medium or hard:")
...
#history
difficultyLevel = input("Please select a difficulty \
level for the  history quiz:easy, medium or hard:")
...

3.user answer questions / recode score

#maths
if difficultyLevel == "Easy" or difficultyLevel == "easy": 
    for x in range(0,3):
        print(questionsForMaths[x].rstrip()) 
    userAnswer = input("Choose from the following:").lower()
    if userAnswer == questionsForMaths[1].rstrip():
        print ("correct")
        score = score + 1 
    else:
        print ("incorrect")
    ....
    (other 4 questions)
 ...
 (other difficulty level)

#history
if difficultyLevel == "Easy" or difficultyLevel == "easy":
   for y in range(0,3):
       print(questionsForHistory[y].rstrip()) 
   userAnswer2 = input("Choose from the following:")
   if userAnswer2 == questionsForHistory[2].rstrip():
       print("correct")
       score = score + 1
   else:
       print("incorrect")
   ....
   (other 4 questions)
...
(other difficulty level)

4.show result

print("Your total score is", score)
percentage = (score/5)*100
print("The percentage of questions correct is", percentage)
if percentage < 40.0:
...
Thus we can first split it to four functions

def open_file(filename):
...
def choose_difficult_level():
...
def answer_questions(topic,difficultyLevel):
...
def show_result(topic, score,fullUsername):
...

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.