CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program that creates a GK quiz consisting of any five questions of your choice. The questions should be displayed randomly. Create a user defined function score() to calculate the score of the quiz and another user defined function remark (scorevalue) that accepts the final score to display remarks as follows:
Marks Remarks
5 Outstanding
4 Excellent
3 Good
2 Read more to score more
1 Needs to take interest
0 General knowledge will always help you. Take it seriously

Open in App
Solution

This program can be easily written using the ‘list’ datatype.

Program:
import random
#this list will store the questions
quiz = ["What is the capital of Uttar Pradesh?","How many states are in North-East India?\nWrite the answer in words.","Which is India\'s largest city in terms of population?","What is the national song of India?","Which Indian state has the highest literacy rate?"]
#the 'ans' list will store the correct answer
ans = ["LUCKNOW","EIGHT","MUMBAI","VANDE MATARAM","KERALA"]

#This list will store the user input
userInp = []

#This list will store the sequence of question displayed
sequence = []

#This list will be storing remarks to display as per the score
remarks =["General knowledge will always help you. Take it seriously", "Needs to take interest", "Read more to score more", "Good", "Excellent", "Outstanding"]

#This user defined function will check the score
def score():
s = 0;
for i in range(0,5):
#Users answer recorded at each index is compared with the answer(using sequence to check the index)
if(userInp[i] == ans[sequence[i]]):
s += 1
return s

#This function will print the remarks as per the score
def remark(score):
print(remarks[score])

#This function will display the question as per the index passed to it
#It will also store the user input and the sequence of the question asked
def disp(r):
print(quiz[r])
inp = input("Answer:")
#User input is recorded after changing it to the upper case
userInp.append(inp.upper())
#The sequence of the question is also recorded, to compare answers later
sequence.append(r)

i = 0;
while i < 5:
#Random number is generated between 0 to 4
r = random.randint(0, 4)
# If that number is not already used, it will be passed to disp function to display the question on that index
if(r not in sequence):
i += 1
disp(r)

#calling score function to the correct answer by each user
s = score()
print("Your score :", s, "out of 5")

#calling remark function to print the remark as per the score
remark(s)


Output:
Which Indian state has the highest literacy rate?
Answer:kerala
Which is India's largest city in terms of population?
Answer:delhi
What is the national song of India?
Answer: vande mataram
What is the capital of Uttar Pradesh?
Answer: lucknow
How many states are in North-East India?
Write the answer in words.
Answer:three
Your score : 3 out of 5
Good

flag
Suggest Corrections
thumbs-up
2
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Introduction to Statistics
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon