Python Practice: Build a Quiz Game from Scratch – Hands-On Coding Project | Navid Ansari | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Python Practice: Build a Quiz Game from Scratch – Hands-On Coding Project

teacher avatar Navid Ansari

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      01 Quiz game introduction

      0:17

    • 2.

      02 Basics of json files

      6:18

    • 3.

      03 Loading json file

      5:26

    • 4.

      04 Ask question

      4:46

    • 5.

      05 Enumerate

      2:39

    • 6.

      06 Get answer from user

      5:13

    • 7.

      07 Run the quiz

      4:59

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

--

Students

--

Projects

About This Class

"Want to master Python while building a fun, interactive project? In this hands-on class, you’ll code a Quiz Game from scratch—perfect for sharpening your Python skills and understanding core programming concepts!

This class is ideal for:
 Python beginners who want to apply their knowledge in a real project
 Aspiring developers looking to strengthen logic & problem-solving
 Teachers or students who want to create quizzes for learning

✨ What You’ll Learn:
 Essential Python concepts (functions, loops, file handling)
 Working with JSON files to store quiz questions & answers
 User input validation & error handling for a smooth experience
 How to structure a Python program like a pro

By the end, you’ll have a fully functional quiz app that you can customize—plus the confidence to tackle more Python projects!

No prior experience? No problem! I’ll guide you step-by-step with clear explanations.

Enroll now and start coding your own quiz game today!

Meet Your Teacher

Teacher Profile Image

Navid Ansari

Teacher

Hello, I'm Navid.

See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 01 Quiz game introduction: This Python practice, we want to create a quiz game. So user will be asked some questions with four options. They will read the question and decide which one of these option is correct. So let's see how we can do that in this practice. 2. 02 Basics of json files: Now, first, let's just create a project and start creating or quiz game. For doing that, I want to go to Desktop in here, go to create a new folder. I want to call it quiz game. Put it over here, go to VS code, go to here and click on Open Folder, go to Desktop and go to Quiz game, Head select folder. In here, we need to create a new file. I will call it quiz dot PY. No, we should decide how we want to store our questions, right? We may have a lot of questions and we want to add them to a file. So every time we can load them and use them, right? So for doing that, I need to create another file. I want to call it Quis questions dot JSON. So I want to use a JSON file for saving all of our questions. And, no, I want to talk more about a JSON file. A JSON file is like a dictionary. You can have key value pair in it. You can have list in it. You can have whatever you want inside it, right? For example, in here, I want to use a key bracket, right? And after that, I want to create a key value pair, for example, the key, I want it to be quins, right? Questions. And the value of these is key, I want it to be a list of dictionary, right? So first, I will create a list, and I put it in multiple line like this. And after that, I can have multiple dictionaries, like this, another dictionary and another dictionary. And I want to put them inside some new lines, so it will be easier to work with, like this. And this one I want it to be new lines, so it will be easier to read. You can see, no, this is exactly like a dictionary that can have a key value pair. So this is just a list of some dictionaries, right, that each one of these dictionaries can have a key value pair, right? The indentation in here doesn't matter. Just these curly brackets and these square brackets are important. For example, in the first dictionary in here, I want to have a key value pair. The key I want it to be question, right. And the value, I want it to be the question itself. So I will say what is the capital of France, right? Christian mark after that? No. For the next key value pair, I don't want to do it on one line. I will go to the next line. And in the next line, I want to add the options. Again, we need the key. So we say options, right? And the value of it, this time, I want it to be list of options, right? So we create a list for the value and we add the options there. For example, London, after that Paris, after that, Berlin, after that Madrid, right? So now we have another key value pair that the key of it is options, and the value of it is a list of strings, right, that is our options. No, for the next key value pair, I want to have the answer to this question as well. So I will create a key and I will call it answer, right? And the value of it, it can be just a string, right? The value of it, I want to say Paris, right? Make sure this is exactly this one that we put in here, right? So we can equal it to see if if they answer it correctly or not. No, for the next dictionary inside this list of questions, I can just copy all of these, and it's really important to copy all of these and paste it in here because the keys should be the same for all of them. And for this question, I want to say which planet is known as the red planet, right? That's it. Now for the options, I see Venus, Mars, Jupiter, Saturn, right? And the answer is Mars, right? No, for the next dictionary in here, I can just again copy and paste all of these over here. For the question, I did copy it from Google. So what is the largest mammal on Earth? For the first option, African elephant. For the second, I will put the blue whale for the third option, giraffe, and for the next option, polar bear. And the answer is blue whale, right? So copy it and paste it over here, right? It's very important that this answer is exactly with the same character as this one, because we want to say if it is equal to this, so you guessed it correctly, right? You answer it correctly, right? So now, we have a JSON file that has some is inside it. And this JSON file is a dictionary that has a key value pair inside it. The key is Christians, and the value is a list of dictionary. And these dictionaries inside this list has a key value pair as well. That first key is Christian, and after that, the value is the Question itself. After that, we have options as a key. And for the value, we have a list of strings. And after that, we have a answer key, and we have a string as a value of it. And we repeat it three times. So we have some questions to work with. That is how we create JSON five. 3. 03 Loading json file: Know that we have a JSON file that has a list of questions inside it. Let's see how we can load it. So for doing that, I will go to quiz dot PY. First and first, I want to import the JSON, right? Because we want to work with JSON. After that, I want to create a function for that, right? So our code will be organized. So in here, I will say dev. Load quiz shins, right? And with that, first and first, I want to add some documentation. So add tree, double quotation on left and right side, and inside it, I want to say load quiz shins from Jason file, right? And after that, right off the bat, I want to use try except. So our application won't crash at all, right? It's really important. So we say try and we want to open a file. So we say Wat Open what file we want to open? We want to open question dot JSON, right? You can just go here, click on here, headf two to rename it and Control C to copy it and in here, paste it, right? It will be much better to do it this way so you don't make any mistake. In here, we want to read this file, and by default, it is on read mode, but it's always a good idea to put the R character there. So you won't make any mistake. And we want Python to give this File to us as this variable name. For example, let's just call it Pi, right? Or we could just call it Christian File. This is just a variable that you can name it whatever you want. After that, we can say JSON dot Load open and close prances file, right? So what this will do, the JSON library will go through this file and it will grab everything from it. And what this will give us, it will give us an object that we can save it inside the variable. And I want to call this variable data, right. And with that done, I want to show that data. So let's just say print. Data, right? Let's see what it will give us. But this try give us a error. So that's why we need to add a except, for example, file not found error, in this case, print, right. Let's just say error, file not found, right? That's it. No, let's just test that. For testing dot, I want to go here and use that function, right? It's called load Questions, open and close prances, and that's it. Now, we can go here, go to terminal, and create a new terminal. And in here, I can say PY, head tab after that, it will put the questions that JSON there, head tab one more time, and it will put the quiz, that PY there, head Inter. No, you can see. I did give us all the text inside the JSON file. And as you can see, it is a dictionary, and inside that dictionary, we have a list of another dictionary inside, and that's how the dictionary work, right? But we don't need the whole dictionary. We need to just load the questions. So with these key questions, we can get this list of questions, right? So let's see how we can do that. In here, instead of saying printing data, we can say, give me just the value of the questions. So we have questions in here that is a key and we want just the value, this list of questions. Nothing more, right? So with these data questions, it will give us this list, right, because the key is questions. The key is questions, and the value is this list. So with that, let's see what will happen. If we run, you can see, no, it is giving us a list of dictionary of questions and answers, right? Cool. No, what I want to do, I don't want to show it in here. I want to return it. So we don't need this print. Let's just delete that and we want to just say return, right? Return the data is. So it will give us the list of questions, not a dictionary, right? Next thing that I want to do, I want to add more except in here because sometimes when you create your JSON file, you made a mistake in there. For example, you forgot to do, for example, this colon in here. And it will give you an error when you say json dot load. So that's why I want to add another except in here except this one, I want it to be Jason dot JSON decoder error. So with that done, colon, and after that, let's just show print error in valid JSON format, right? That's it. No, whatever error that this function gives us, we are handling it with these two except. If it's through more errors, we can get that error and do something with it, right? No, we are loading the questions. 4. 04 Ask question: Know that we are loading the questions. Let's see how we can ask a question. So for doing that, first of all, we are loading a question, and with that, we are returning something, right? So in here, I want to save that return inside is, right? Questions is equal to this. This is just a variable that we are saving all the questions inside it. Now, if you print these questions, let's see what it will give us, right? Questions. Control to save, run no, you can see it is giving us a list of questions, right? So with that, I want to create a function for asking a question. So in here at top, I will say def ask Question, right? And in there, I want to have an input to tell it which question to ask because right now we have three questions, right? So we say Question data, right, or whatever you want to call it. After that, let's just make some space. And this ask question. First thing that I want to do, I want to add the documentation, right? Like this we three double quotation. Display a question and get the user answer, right? That's it. Now, first and first, I want to print the question. So I will say print. And right off the bat, I wanted to go to New line. So use backlash N, so it will go to New line, so it will be more cleaner. After that, let's just change this to a f string and show the question with a coll bracket, right? So if you look at here, we want to, for example, show this one, right? What is the capital of France, right? So how we can get that because it is inside a dictionary, it is a key value pair. We can use the key to get the value, right? So in here, we can say question data, and we want to get the question. We can just copy it from here to here like this. So with this, it will get this value pair, for example, right? I will tell you how you can ask the other questions as well because we have three questions. But with this data, with this key question, we are getting the value of it and we show it. Now let's see how we can use this function. For using this function, what we can do, we can say ask question, and we need to give it a input, right? What is the input? We can say questions number zero, for example, right? So we are passing the question number zero in this list, right from here to here, right? Now, let's just delete this. We don't need it. Now, let's just run, and you can see it says, What is the Capital of France? That's beautiful. We are showing the actual question. What is the capital of France? No, we want to get the options, right from this data. How we can do that? I want to show you two way of doing this, so it will be a practice for you. First thing, I want to do it with a counter. So I will say index is equal to zero, right? After that, I want to use a four loop for getting all the options of this question. So we say for option in question data, know what you want to get, we want to get the options. So in here, the key is option and the value is these ones, right? So let's just pass this key there, so it will give us the values and we are looping through them, right? No, with the ton because index is starting from zero, but we want to number the question from first, we say index is plus equal to one. So we add one to it, right? And after that, we say print with an FS string. First, I will show the index with a curly bracket. After that, I will say dot, and after that space, and after that, another curly bracket and show the option. Now, let's see what will happen. Control S to save. Now in here, let's just run and now you can see it is saying, What is the capital of France. And it says one is London, two is Paris, three is Berlin, and four is Madrid. That's beautiful, right? This was one way to do it, but I want to show you another way that maybe many professionals use. Let's see how we can do that. Next. 5. 05 Enumerate: See, we did do this with a four loop with an index that we are manually adding to it, but there is other ways as well. Let me comment this out. So holding ln sheet, drag and drop from top to bottom, and add a hash tag at the beginning of them, it will be comment, and it won't be run by Python. If you like this way of doing it, I like this way of doing it, you can just ignore this video. This video is just about the new things, right? For doing that, we say four, I and option. In enumerate this time, right? So this enumerate will get input of something that is a ter like a list, and it can add index to it, right? And it will give us that index inside this I. We can just call this index, right, so you will understand it with this one better. So now if you pass the data, the options of these questions data, we can just copy the options from here and paste it over here, right? Control C control V. Now what this enumerate do it will give each one of these options inside these options, list an index. If you go to JSON, you can see, we have a key that is options, and for the value of it, we have a list of strings, right? But these for loop, we don't have any index. We use enumerate to give them index. This is index zero, this is Index one, this is Index two, and this is index three, right? So by default, four loop won't give us the index of a list, right? But with the enumerate, we are giving each one of the items inside a list a index, just like that. We say enumerate and after that, pass the list to it. Now with that done, we have access to index and option. And with that, we can just print a tote. We can say print. I want to use FSString as well for the number because the enumerate starts from zero. That's why we want to say index plus one. So it will start from one and after that, a dot, and after that, show the option there, right? That's it. That's beautiful. Less line, right? And we are doing things automatically not manual increasing values, right? If we run again, you can see the result is exactly the same, but we did it in a professional way. 6. 06 Get answer from user: No, we want to get the answer from the user and to see if they answer correctly or not. So for doing that, what I want to do I want to create a vile through in here, right? So we get the correct answer or wrong answer, right? And with that, we say fresh try, right? We want to catch any error, so our application don't get to be crashed, right? So we say Troy. We need something from input, right? So we say input and Inder. I want to say Inter your answer. Let's give it a hint. It should be between one, two, four, after that a colon and after that space. And this input give us a string. We need to convert it to a integer. We say int, and we wrap it around prenss and we need to save it somewhere. I will create a variable and I will call it choice is equal to this, right? First and first, I want to check this choice if it is 1-4. So I will say, if choice. Is bigger or equal than one, and choice is smaller or equal than four. In that case, they put in range number. With that done, we want to get that option to see if it's the answer or not. So for doing that, let me go to questions. You can see we have the options, and they select one of them, one, two, three, four, right? First, we need to go through this dictionary. The keys options, get the options. The value of this key option is a list, and no we want to get the one that they choose inside these options and check if it is equal to answer. Let's see how we can do that. We'll say question data. First, we need to get the key, the keys options, right, and paste it over here. And after that, this will give us a list, right. You can see the values. The value of this key is a list. So again, we need another one of these and put the choice there because users see the choice that is a start from one, but our list is starting from zero. That's why we need to minus this by one, right? Subtracted by one. So with that, it will give us a string that we want to save it inside a variable. I will call this variable, selected answer, right? Is equal to this. Now with that done. We want to return if the selected answer is the answer or not. So we say return the selected answer if it is equal to the answer that is saved in or question data. So we say data, and we want to get the answer from it. If you go here, you can see the key is answered, so copy it and in here, paste it, right? So what will happen? If the selected answer was equal to the answer saved, it will return true. And if it's not, it will return false. Now with that done, what will happen. If they choose a value 1-4, we check if the answer is correct or not. But if they choose a value that is not 1-4, we say else, right? And in here, we want to say print. For example, error, please write a number between one, two, four, right? That's it. No, we need to complete this try except, right? So go down. In this line of try, we say except. I want to use the value error because the value error can happen in here. After that, if this value error happen, again, we want to print something. I want to say, please write a valid number. And I want to add error at the beginning of it. Error just like that, right? Control is to save. Now we have a function that can ask a question and tell us if it is correct or not. We are asking the question zero. Let's just show the result with a print and after that open and close parents around the ask questions. So we just showing true or false, right? Now with that done, let's just go here and run, make sure you save no, if we put N in there, what will happen, it says, please write a valid number. If you put five in there, it will say, please write a number 1-4, right? If we put four in there, what will happen, it says, false. Your answer is false. Let's just run it one more time. And this time, let's just answer it correctly. Paris, head, Enter. No, you can see it is giving us true. No, we did create a function that can ask us questions and get the answer. Beautiful. 7. 07 Run the quiz: No, I want to create a function that ask all the questions and take some score, right? So let's see how we can do that. In here, I want to create a function. I will call it run, quiz, right? And because it is function, it needs to open and close prances. And after that on add comment, main function, to run the quiz, right? After that, I don't want to load the questions in here or ask the questions in here. I want to do it inside these functions, so everything will be clear. You can see we have some function, cool function that we can collapse, and it's easy to read, right? So knowing here, first thing first, we need to load the questions, right? So we say load questions and we want to save it inside the questions. Right? That's it. Now we have all the questions there. Of course, call it questions, not question because it will give us a list of questions. First thing that I want to check, I want to check. If there is no questions returned, maybe in JSON file, we don't have any questions, right? If not questions, it means if there is no question inside these questions, variable, print, no question avapo. And after that, we need to return and don't continue this function. But if there is some questions inside these questions, first, I want to create a variable. I want to call it a score, and I want to put it to zero for now. After that, I want to use a print to tell the user what we are going to do in here. I will say, welcome to the quiz. Game. After that, I want to use another print. This time, I want to use a string. I don't want to say, you will be asked. I want to get the length of question to tell them how many questions they should answer. We say n s, right? Question. And at the end, let's say, let's begin. And after this, let's begin, I wanted to go to a new line. With these two line, we have a a space between them. I wanted to show that you can use this backslash N at the end of the string, as well. No, I want to use a four loop for question in questions, right, what we want to do? We want to ask question right in here. We say ask question, and we pass the question there. Okay, cool, right? And what this skill will give us, it will give us a true or false. And we can save it in the variable, and I want to call it is correct, right? Is equal to this ask question. After that, I want to check it with the I I is correct, right? If it was correct, we want to increase the score by one plus equal to one. After that, I will say print, correct, right? But in case of else, what we want to do, print wrong. When this flop finished, it means it did ask all the questions from the user and now we want to show the results. So in here, I want to use a print, right? I want to use f string in here. First, I want to have a space between the questions and this line. So that's why I will use backslash N. I want to say quiz, completed your score is put curly bracket there, show the score there. And we want to tell it how many is score from how many. So we add a slash in here and we show the length of the quiz. So we say n quiz shins, right? No, with that done, we need to run this function, right? So we go down in here. We say run quiz. It is a function. So let's just add open and closed prances, make sure you save and no let's just run. It says, Let me make this bigger so you can see everything. From here. Welcome to Quiz Game. You will be asked three questions. Let's begin. What is the capital of France? Let's say two, Paris, right? It will say correct. After that, it will say which planet is known as the red planet? Let's just say Venice, right? So we have a wrong answer. Head inter, and it says wrong. And no, it says, What is the largest mammal on Earth? Yeah, it is blue whale. So let's just put two in there, and now you can see it is saying correct. After that, it says, Quiz completed. Your score is two of three. Beautiful.