forked from shriyaa01/Python_Quiz_Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_game.py
136 lines (136 loc) · 5.17 KB
/
quiz_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import random
import sys
import os
#Question to be asked
quiz_data = [
{
"question": "What does HTML stand for?",
"answer": "Hypertext Markup Language"
},
{
"question": "Which programming language is often used for web development?",
"options": ["A. Python", "B. Java", "C. Ruby", "D. JavaScript"],
"answer": "D"
},
{
"question": "What is the primary function of CSS in web development?",
"options": ["A. Data storage", "B. Styling and layout", "C. Server-side scripting", "D. Database management"],
"answer": "B"
},
{
"question": "What does the acronym SQL stand for?",
"options": ["A. Structured Query Language", "B. Simple Query Language", "C. Standard Query Language", "D. Sequential Query Language"],
"answer": "A"
},
{
"question": "Which programming language is often used for data analysis and scientific computing?",
"options": ["A. JavaScript", "B. Java", "C. Python", "D. C++"],
"answer": "C"
},
{
"question": "What does API stand for in the context of web development?",
"options": ["A. Application Programming Interface", "B. Advanced Programming Interface", "C. Automated Program Interaction", "D. All of the above"],
"answer": "A"
},
{
"question": "In Python, which keyword is used to define a function?",
"options": ["A. define", "B. func", "C. def", "D. function"],
"answer": "C"
},
{
"question": "What is the output of the following code: `print(3 * 'Hello ')`?",
"options": ["A. Hello Hello Hello Hello", "B. 9", "C. Hello Hello Hello", "D. Syntax Error"],
"answer": "C"
},
{
"question": "In JavaScript, how do you declare a variable?",
"options": ["A. var", "B. variable", "C. let", "D. declare"],
"answer": "A"
},
{
"question": "Which data type in Python is used to represent a sequence of characters?",
"options": ["A. int", "B. float", "C. str", "D. list"],
"answer": "C"
},
{
"question": "What is the primary purpose of version control systems like Git?",
"options": ["A. To write code", "B. To run code", "C. To manage and track changes in code", "D. To execute code"],
"answer": "C"
},
{
"question": "What is the main advantage of object-oriented programming (OOP)?",
"options": ["A. Simplicity", "B. Reusability", "C. Procedural nature", "D. No need for functions"],
"answer": "B"
},
{
"question": "What is the HTTP status code for a successful response?",
"options": ["A. 200 OK", "B. 404 Not Found", "C. 500 Internal Server Error", "D. 302 Found"],
"answer": "A"
},
{
"question": "In Python, which library is commonly used for data visualization?",
"options": ["A. NumPy", "B. Pandas", "C. Matplotlib", "D. TensorFlow"],
"answer": "C"
},
{
"question": "What is the purpose of a constructor method in object-oriented programming?",
"options": ["A. To create objects", "B. To destroy objects", "C. To update objects", "D. To copy objects"],
"answer": "A"
},
{
"question": "Which of the following is not a valid Python data type?",
"options": ["A. int", "B. double", "C. list", "D. tuple"],
"answer": "B"
},
{
"question": "What is the result of the following Python code: `3 + '3'`?",
"options": ["A. 6", "B. '33'", "C. TypeError", "D. '6'"],
"answer": "C"
},
{
"question": "Which programming language is often used for developing mobile applications?",
"options": ["A. Java", "B. Python", "C. C#", "D. PHP"],
"answer": "A"
},
{
"question": "What is the purpose of the `if` statement in programming?",
"options": ["A. To perform a loop", "B. To declare a function", "C. To make decisions based on conditions", "D. To define a class"],
"answer": "C"
}
]
def clear():
os.system('cls' if os.name == 'nt' else "clear")
def pause():
try:
os.system('pause')
except:
input("계속하려면 아무 키나 누르십시오 . . . ")
sys.stdout.write('\x1b[1A')
sys.stdout.write('\x1b[2K')
#displaying content
def ask_question(question_data):
print(question_data["question"])
if "options" in question_data:
for option in question_data["options"]:
print(option)
user_answer = input("Enter your answer (A,B,C,D): ").upper()
else:
user_answer = input("Enter your answer: ")
if user_answer == question_data["answer"]:
return True
else:
return False
#main body
if __name__=="__main__":
score=0
random.shuffle(quiz_data)
for i in range(1,6):
clear()
print(f'Question {i} of 5')
if ask_question(quiz_data[i]):
print("Correct!\n")
score += 1
else:
print(f"Wrong! The correct answer is {quiz_data[i]['answer']}.\n")
pause()
print(f"You scored {score}/5.")