├── assets ├── user_accounts.json └── questions.json ├── README.md ├── LICENSE └── quiz.py /assets/user_accounts.json: -------------------------------------------------------------------------------- 1 | { 2 | "Aman": ["Aman", "ADMIN"], 3 | "aman": ["1234", "PLAYER"] 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quiz Application 2 | 3 | A simple Quiz application in Python programming lanaguage completed as a mini project. 4 | 5 | ## Usage 6 | 7 | ```powershell 8 | python quiz.py 9 | ``` 10 | 11 | ## Features 12 | 13 | - Admin and Player 14 | 15 | - Login as **admin** to add more questions 16 | 17 | - Login as **player** to play quiz 18 | 19 | ## Team 20 | 21 | 22 | 23 | ## License 24 | 25 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Aman Khadka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /quiz.py: -------------------------------------------------------------------------------- 1 | # JSON is a syntax for storing and exchanging data. 2 | # Python has a built-in package called json, which can be used to work with JSON data. 3 | import json 4 | # Python Random module is an in-built module of Python which is used to generate random numbers. 5 | import random 6 | # No echo input ( passwords aren't shown ) 7 | import getpass 8 | 9 | user = [] 10 | 11 | def play(): 12 | print("\n==========QUIZ START==========") 13 | score = 0 14 | with open("assets/questions.json", 'r+') as f: 15 | j = json.load(f) 16 | for i in range(10): 17 | no_of_questions = len(j) 18 | ch = random.randint(0, no_of_questions-1) 19 | print(f'\nQ{i+1} {j[ch]["question"]}\n') 20 | for option in j[ch]["options"]: 21 | print(option) 22 | answer = input("\nEnter your answer: ") 23 | if j[ch]["answer"][0] == answer[0].upper(): 24 | print("\nYou are correct") 25 | score+=1 26 | else: 27 | print("\nYou are incorrect") 28 | del j[ch] 29 | print(f'\nFINAL SCORE: {score}') 30 | 31 | def quizQuestions(): 32 | if len(user) == 0: 33 | print("You must first login before adding questions.") 34 | elif len(user) == 2: 35 | if user[1] == "ADMIN": 36 | print('\n==========ADD QUESTIONS==========\n') 37 | ques = input("Enter the question that you want to add:\n") 38 | opt = [] 39 | print("Enter the 4 options with character initials (A, B, C, D)") 40 | for _ in range(4): 41 | opt.append(input()) 42 | ans = input("Enter the answer:\n") 43 | with open("assets/questions.json", 'r+') as f: 44 | questions = json.load(f) 45 | dic = {"question": ques, "options": opt, "answer": ans} 46 | questions.append(dic) 47 | f.seek(0) 48 | json.dump(questions, f) 49 | f.truncate() 50 | print("Question successfully added.") 51 | else: 52 | print("You don't have access to adding questions. Only admins are allowed to add questions.") 53 | 54 | def createAccount(): 55 | print("\n==========CREATE ACCOUNT==========") 56 | username = input("Enter your USERNAME: ") 57 | password = getpass.getpass(prompt= 'Enter your PASSWORD: ') # The getpass() function is used to prompt to users using the string prompt and reads the input from the user as Password. 58 | with open('assets/user_accounts.json', 'r+') as user_accounts: #The r+ mode is used to open a file for both reading and writing 59 | users = json.load(user_accounts) 60 | if username in users.keys(): 61 | print("An account of this Username already exists.\nPlease enter the login panel.") 62 | else: 63 | users[username] = [password, "PLAYER"] 64 | user_accounts.seek(0) # seek() function is used to change the position of the File Handle to a given specific position. 0 = beginnning 65 | json.dump(users, user_accounts) #dumps convert python objects of the all types such int,float,list,tuple,dictionary into JSON strings: 66 | user_accounts.truncate() # Python file method truncate() truncates the file's size. 67 | print("Account created successfully!") 68 | 69 | def loginAccount(): 70 | print('\n==========LOGIN PANEL==========') 71 | username = input("USERNAME: ") 72 | password = getpass.getpass(prompt= 'PASSWORD: ') 73 | with open('assets/user_accounts.json', 'r') as user_accounts: 74 | users = json.load(user_accounts) 75 | if username not in users.keys(): 76 | print("An account of that name doesn't exist.\nPlease create an account first.") 77 | elif username in users.keys(): 78 | if users[username][0] != password: 79 | print("Your password is incorrect.\nPlease enter the correct password and try again.") 80 | elif users[username][0] == password: 81 | print("You have successfully logged in.\n") 82 | user.append(username) 83 | user.append(users[username][1]) 84 | 85 | def logout(): 86 | global user 87 | if len(user) == 0: 88 | print("You are already logged out.") 89 | else: 90 | user = [] 91 | print("You have been logged out successfully.") 92 | 93 | def rules(): 94 | print('''\n==========RULES========== 95 | 1. Each round consists of 10 random questions. To answer, you must press A/B/C/D (case-insensitive). 96 | Your final score will be given at the end. 97 | 2. Each question consists of 1 point. There's no negative point for wrong answers. 98 | 3. You can create an account from ACCOUNT CREATION panel. 99 | 4. You can login using the LOGIN PANEL. Currently, the program can only login and not do anything more. 100 | ''') 101 | 102 | def about(): 103 | print('''\n==========ABOUT US========== 104 | This project has been created by Aman Khadka. 105 | It is a basic Python Project for my 1st Semester.''') 106 | 107 | if __name__ == "__main__": 108 | choice = 1 109 | while choice != 7: 110 | print('\n=========WELCOME TO QUIZ MASTER==========') 111 | print('-----------------------------------------') 112 | print('1. PLAY QUIZ') 113 | print('2. ADD QUIZ QUESTIONS') 114 | print('3. CREATE AN ACCOUNT') 115 | print('4. LOGIN PANEL') 116 | print('5. LOGOUT PANEL') 117 | print('6. SEE INSTRUCTIONS ON HOW TO PLAY THE GAME') 118 | print('7. EXIT') 119 | print('8. ABOUT US') 120 | choice = int(input('ENTER YOUR CHOICE: ')) 121 | if choice == 1: 122 | play() 123 | elif choice == 2: 124 | quizQuestions() 125 | elif choice == 3: 126 | createAccount() 127 | elif choice == 4: 128 | loginAccount() 129 | elif choice == 5: 130 | logout() 131 | elif choice == 6: 132 | rules() 133 | elif choice == 7: 134 | break 135 | elif choice == 8: 136 | about() 137 | else: 138 | print('WRONG INPUT. ENTER THE CHOICE AGAIN') -------------------------------------------------------------------------------- /assets/questions.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "question": "When does Nepal Celebrate New Year?", 4 | "options": [ 5 | "A. 1st Baisakh", 6 | "B. 2nd Baisakh", 7 | "C. 29th Chaitra", 8 | "D. 30th Chaitra" 9 | ], 10 | "answer": "A. 1st Baisakh" 11 | }, 12 | { 13 | "question": "Nobel prize is awarded for which of the following disciplines?", 14 | "options": [ 15 | "A. Literature, peace and economics", 16 | "B. Medicine or Physiology", 17 | "C. Chemistry and Physics", 18 | "D. All the above" 19 | ], 20 | "answer": "D. All the above" 21 | }, 22 | { 23 | "question": "How many provinces are there in Nepal?", 24 | "options": ["A. 1", "B. 7", "C. 14", "D. 75"], 25 | "answer": "B. 7" 26 | }, 27 | { 28 | "question": "Which country operationalized world’s largest radio telescope?", 29 | "options": ["A. USA", "B. China", "C. Russia", "D. India"], 30 | "answer": "B. China" 31 | }, 32 | { 33 | "question": "Which one among the following radiations carries maximum energy?", 34 | "options": [ 35 | "A. Ultraviolet rays", 36 | "B. Gamma rays", 37 | "C. X- rays", 38 | "D. Infra-red rays" 39 | ], 40 | "answer": "B. Gamma rays" 41 | }, 42 | { 43 | "question": "The head quarters of world trade organization is in?", 44 | "options": [ 45 | "A. Montreal", 46 | "B. Seattle", 47 | "C. Geneva", 48 | "D. None of the above" 49 | ], 50 | "answer": "C. Geneva" 51 | }, 52 | { 53 | "question": "The language spoken by the people by Pakistan is ?", 54 | "options": ["A. Hindi", "B. Nepali", "C. English", "D. Sindhi"], 55 | "answer": "D. Sindhi" 56 | }, 57 | { 58 | "question": "At the end of March 2019, what was the amount of India's external debt?", 59 | "options": [ 60 | "A. US $ 540 billion", 61 | "B. US $ 543 billion", 62 | "C. US $ 547 billion", 63 | "D. US $ 541 billion" 64 | ], 65 | "answer": "B. US $ 543 billion" 66 | }, 67 | { 68 | "question": "'LIBRA' an alternative of existing cryptocurrency like bitcoin is being developed by?", 69 | "options": [ 70 | "A. Facebook", 71 | "B. Google", 72 | "C. Microsoft", 73 | "D. None of the above" 74 | ], 75 | "answer": "A. Facebook" 76 | }, 77 | { 78 | "question": "Galileo was an astronomer who?", 79 | "options": [ 80 | "A. developed the telescope", 81 | "B. discovered four satellites of Jupiter", 82 | "C. discovered that the movement of pendulum produces a regular time measurement", 83 | "D. All the above" 84 | ], 85 | "answer": "B. discovered four satellites of Jupiter" 86 | }, 87 | { 88 | "question": "Headquater of International Olympic Committee is situated at?", 89 | "options": ["A. Athens", "B. Lausanne", "C. Dubai", "D. None of the above"], 90 | "answer": "B. Lausanne" 91 | }, 92 | { 93 | "question": "'The Coalition Years' is the autobiography of?", 94 | "options": [ 95 | "A. L.K Advani", 96 | "B. Pranab Mukherjee", 97 | "C. Atal Behari Vajpayee", 98 | "D. Sonia Gandhi" 99 | ], 100 | "answer": "B. Pranab Mukherjee" 101 | }, 102 | { 103 | "question": "The metal whose salts are sensitive to light is?", 104 | "options": ["A. Silver", "B. Zinc", "C. Copper", "D. Gold"], 105 | "answer": "A. Silver" 106 | }, 107 | { 108 | "question": "Who is the father of geometry?", 109 | "options": ["A. Aristotle", "B. Euclid", "C. Pythagoras", "D. Kepler"], 110 | "answer": "B. Euclid" 111 | }, 112 | { 113 | "question": "Entomology studies what?", 114 | "options": [ 115 | "A. Behavior of human beings", 116 | "B. Insects", 117 | "C. The origin and history of technical and scientific terms", 118 | "D. The formation of rocks" 119 | ], 120 | "answer": "B. Insects" 121 | }, 122 | { 123 | "question": "Galileo was an astronomer who", 124 | "options": [ 125 | "A. Developed the telescope", 126 | "B. Discovered four satellites of Jupiter", 127 | "C. discovered that the movement of pendulum produces a regular time measurement", 128 | "D. All the above" 129 | ], 130 | "answer": "B. Discovered four satellites of Jupiter" 131 | }, 132 | { 133 | "question": "Who is the father of geometry?", 134 | "options": ["A. Aristotle", "B. Euclid", "C. Pythagoras", "D. Kepler"], 135 | "answer": "B. Euclid" 136 | }, 137 | { 138 | "question": "Indian player Jude Felix is popular for which sports?", 139 | "options": ["A. Volleyball", "B. Football", "C. Hockey", "D. Tennis"], 140 | "answer": "C. Hockey" 141 | }, 142 | { 143 | "question": "Who is the current chief of the world bank group?", 144 | "options": [ 145 | "A. Donald Tusk", 146 | "B. David Malpass", 147 | "C. Christine Lagarde", 148 | "D. Jim Yong Kim" 149 | ], 150 | "answer": "B. David Malpass" 151 | }, 152 | { 153 | "question": "When was Second World War started?", 154 | "options": [ 155 | "A. September 1, 1939", 156 | "B. September 1, 1940", 157 | "C. September 1, 1945", 158 | "D. September 1, 1942" 159 | ], 160 | "answer": "A. September 1, 1939" 161 | }, 162 | { 163 | "question": "Each year World Red Cross and Red Crescent Day is celebrated on?", 164 | "options": ["A. May 8", "B. May 18", "C. June 8", "D. June 19"], 165 | "answer": "A. May 8" 166 | }, 167 | { 168 | "question": "The brain fever which affects young children is?", 169 | "options": ["A. Malaria", "B. Typhoid", "C. Encephalitis", "D. Pneumonia"], 170 | "answer": "C. Encephalitis" 171 | }, 172 | { 173 | "question": "Which is the first search engine of the internet?", 174 | "options": ["A. Yahoo", "B. Wais", "C. Archie", "D. Mozilla"], 175 | "answer": "C. Archie" 176 | }, 177 | { 178 | "question": "In a Database Management System(DBMS) the content and the location of data is identified by ?", 179 | "options": [ 180 | "A. Subdata", 181 | "B. Sequence Data", 182 | "C. Beta Data", 183 | "D. Meta Data" 184 | ], 185 | "answer": "D. Meta Data" 186 | }, 187 | { 188 | "question": "'.MOV' extensions refer to which kind of file?", 189 | "options": [ 190 | "A. Image file", 191 | "B. Movie File", 192 | "C. Audio File", 193 | "D. ZIP File" 194 | ], 195 | "answer": "B. Movie File" 196 | }, 197 | { 198 | "question": "Who wrote the famous poem 'The Charge of the Light Brigade'?", 199 | "options": [ 200 | "A. Lord Alfred Tennyson", 201 | "B. Christopher Marlowe", 202 | "C. Johannes Gutenberg", 203 | "D. Rene Descartes" 204 | ], 205 | "answer": "A. Lord Alfred Tennyson" 206 | }, 207 | { 208 | "question": "Who founded Indian National Army(INA)?", 209 | "options": [ 210 | "A. Netaji Subhas Chandra Bose", 211 | "B. Mohan Singh", 212 | "C. Rash Behari Bose", 213 | "D. Jatridranath Mukherjee" 214 | ], 215 | "answer": "B. Mohan Singh" 216 | }, 217 | { 218 | "question": "The control of which state was handed over to british after marriage of King Charles 2 with portuguese princess in 1661?", 219 | "options": ["A. Cochin", "B. Bombay", "C. Surat", "D. Madras"], 220 | "answer": "B. Bombay" 221 | }, 222 | { 223 | "question": " Country that has the highest in Barley Production ?", 224 | "options": ["A. China", "B. India", "C. Russia", "D. France"], 225 | "answer": "C. Russia" 226 | }, 227 | { 228 | "question": "The World Largest desert is ?", 229 | "options": ["A. Thar", "B. Kalahari", "C. Sahara", "D. Sonoran"], 230 | "answer": "C. Sahara" 231 | }, 232 | { 233 | "question": "Guru Gopi Krishna is popular for which form of Indian dance?", 234 | "options": ["A. Bharatanatyam", "B. Kuchipudi", "C. Kathak", "D. Manipuri"], 235 | "answer": "B. Kuchipudi" 236 | }, 237 | { 238 | "question": "J.B. Dunlop is popular for which of the following inventions?", 239 | "options": [ 240 | "A. Model airplanes", 241 | "B. Pneumatic rubber tire", 242 | "C. Rubber boot", 243 | "D. Automobile wheel rim" 244 | ], 245 | "answer": "B. Pneumatic rubber tire" 246 | }, 247 | { 248 | "question": "Who built this project ?", 249 | "options": ["Aman", "Nutan", "Shyam", "Ram"], 250 | "answer": "Aman" 251 | } 252 | ] 253 | --------------------------------------------------------------------------------