Python 8.4: Managing a score
We will continue our questionnaire project and here we will introduce the score to do this I'm going to show you several approaches and this will be an opportunity for us to talk about global variables we'll start by creating the score variable at this level I'll make score equal to zero I'll initialize it, and we'll put a print at the end of the print function here of the print function here then we'll make the function ask question return either true or false so a boolean at this level I'll put return True and return False But it's this that's a trap because when it's going to check the right answer if it's correct know how to send too much and it's not going to execute the print ... and if the answer is not true it will enter the else and it will execute the print of the else and return false Without ever entering the print of the bottom of the blow this approach is not So we're going to do something else I'm going to create a variable correct answer equal to false so I'm initializing it And if I delete this already and if The answer is equal to the chosen answer so is equal to the correct answer, we're going to put correct answer I forgot the equality you're going to tell me that no we could do like If the answer is equal to the chosen answer we put true so correct answer goes to thing and if it's not the case correct answer goes to false this I put this down here But I do it another way well if we first have to initialize the correct answer before changing the value so I'll redo what I had like this and here we'll do an if for the score If the answer is correct two points the score will be incremented plus equal to an nn covit. and we do the same thing if two points like this I rerun my code before that I'll display the score at the bottom by doing a print final score colon score I rerun I'll put a here I will put c we could do this method that I just did here it really works But here python is a very strict language as far as these things are concerned so for the score to be oncremented in the function we would have to add at this level score I'm going to delete this first which was another approach I'm going to add score plus 1 like this you can see that there is an error at this level because the function doesn't recognize the score variable and so that it can access the score variable which is in the main program, we need to put global score here like this you can see that there is no more error and at the if level we can see that we have copied the score several times We have copied the score twice to put it in the other function And let's imagine if we have 100 questions we'll do the same thing each time, that's why... I preferred to put. score itself, inside the function itself Here I'm going to delete this which is no longer of any use because the other method does not use it It was necessary to declare the score at the top I delete And here I delete this too I'm going to delete this I delete there's no more error And I restart my code I put c and I put a That it is displayed normally I restart and I will miss a question I put c I put c also you see that the score gives normally therefore There is no error The ambiguity was at the level of the variable Considering that we had declared the variable score in the principal program and that To reach this variable we had to come to the global it is for that that it had muance Here there was a problem because. We want to rewrite or reassign a new value to the score if for example. At this level I put rather Comment this and here I put 10 I comment this and I make print of score I restart my program You see that the print, wait we will make I will put this is the score colon I relaunch you see that displays well this is the score What means that the function can access the data of a Variable Even if it is not declared global But it is when we want to rewrite or reincrement. So that's all for this video and we'll see you soon for another one. The expertise of the functions in more improved version.