Python Tutorial for Beginners – Crash Course 2020 | Build a Game with Python
- March 8, 2024
- Posted by: MainInstructor
- Category: Go Python React Web Development
Video Title: Python Tutorial for Beginners – Crash Course 2020 | Build a Game with Python
Hi great to have you aboard of this video do you want to learn – then why don’t you learn it by creating something entertaining and funny a game for example this is exactly what we will do in this video we will build a game in Python from scratch no prior Python
Knowledge is required the game will run in the terminal and in this game we as the player will fight against the monster we will be able to create a player name we can attack the monster we can heal ourselves we can’t even display high scores from previous rounds and
More cool things so if you want to learn – right now this is your chance to do so So before we dive into Python let’s have a look at the actual game that we want to build the game will actually consist of a player as basically and a monster the player has certain attributes you could say for example the player name the player could have an attack so an
Attack which allows us to well harm the monster we could have something like a heal feature which allows us to increase our health level and we also need a health indicator and health status bar which shows us if you are still alive or if we
Are both dead the same thing is true for the monster the monster also could have some attributes like name and the health indicator and also the attack picture let’s say that the monster shouldn’t be allowed to heal itself though so the monster basically only can attack ourselves now to control this entire
Game flow we need something like a user interface which allows us to select an action we are always working from the player perspective of course this means an action we can select is basically only attack or heal so let’s say we want to attack the monster right here if we
Do so we will have an impact on to the health of the monster and decrease the monsters well health status after this attack we have to check if the monster is still alive so if the health level is smaller than or equal to zero if this is
The case so if we say yes the monster is basically not anymore then we have to make sure that the player wins and that a new round of the game starts or a new game starts in general in case the monster is still alive though well the monster will attack back
Automatically with that the monster bill of course affect our health negatively then we have to implement the same check we had before we have to check if our health level is below or equal to zero if that’s the case well then unfortunately the monster won the game
And we have to start a new one if this is not the case so if you are still alive well then we go back to our user interface and we can start the next round by selecting another action in our case this would be attack or heal so that’s
The game we want to build in a nutshell as I said we will build this all from scratch with Python and for that we need to use and understand some core Python features so what will we learn in here regarding – we will have a look at variables variables allow us to store
Information in our code and to access it whenever we need it we will also have a look at user input this means we will learn how we can make sure that the user can enter data and that we can react to the input the user made will also have a
Look at operators operators allow us to subtract or add different items or also to check whether an item is smaller or bigger than another item for example besides that we’ll have a look at lists and dictionaries dictionaries are special ways you could say to store information in Python and
Also one of the key concepts we will cover in this video we’ll also have a look at functions in pythons so we will learn how we can create functions what functions are and why we need them to create reusable code definitely one of the core features of every programming
Language basically we’ll also have a look at control structures these are things like if statements for example you might have heard about that this means we execute code if a certain condition is true well or not and finally we’ll have a look at f strings so-called formatted string literals
These are things which allow us to dynamically inject in from outside into a so-called string this is a bit more advanced right here but we will come to a point in the video where this makes perfect sense to you now with that we can leave this theoretical planning part and jump
Directly into our code and to get started you need an IDE in my case I will use Visual Studio code and you need – free installed on your machine installation guidelines for both of these can be found in the video description now once you created a new
Project in your IDE we can create a Python file right here we click on to this button and now we can give this file any name we want in my case I will name it game the important thing is that we add dot py to this file this will
Turn it into a file – can read them and in this file we can now get started and as I said we need a player and the monster these are the core components of our game you could say and the player and the monster have certain attributes
So if we think about the player we could say that our player should have a name and a name could be manual in this case for example my name now if we save that you see that we get an error immediately if we hover over it you can see that we
Created an undefined variable manual and that’s the first important concept we will cover here writing text like this in our code won’t work here we have to put this information this specific information into a so-called variable variable simply allow us to store information in our code and to access
This information later now to make this a bit more precise right here we could say that actually manual should just be the value or the information that you want to store right here and this information should be stored in a variable which could be named player underscore name for example that’s the
Naming convention in Python for variables we type the name with small letters and if we have a combination of different names like player name in here we connect these different names with an underscore then we add a space and set the variable equal to the information that should be
Stored in this variable in that case the name manual now if we save it now we can see that the error regarding the variable right here is gone because we basically declared that variable so we assigned a value to this variable manual in this case but if I hover over manual
Right here you can see that this is still an undefined variable now the problem is that the information in a variable has to have a specific data type data types in Python are numbers so integers in this case as you can see if I save this now we don’t have an error
These are floats like 1 or 2 for example this is also working we have boolean x’ a boolean would be true like that written with a capital letter at the beginning or false written like that or as we have in our case strings strings should be wrapped
In quotation marks this can be double quotation marks like this or single rotation marks generally you can use whichever you want the important thing is to be consistent though I will stick to single quotation marks in this project here now we can add the player name right here in the petition marks
Manual and with that you can see that we now don’t get an error and that manual now is a string so summarize that we now declared a variable named player name and this variable contains information in this case the string manual that’s basically our first variable now we can
Of course create additional variables here another one could be player attack for example we talked about that already and this could have an integer now let’s say 10 we could also add player he’ll for example that we can heal our player which could be a value of 16 we might
Have to work on these values later and we could add health for example right here as the last variable which could have a value of 100 so with that we basically defined our player right here this doesn’t do a lot on if we now execute the code that we wrote in this
File we will see that nothing is displayed down here in our terminal this is what I talked about at the beginning of this video that our user interface will be this part right here this terminal by the way if you don’t see this terminal you can always make it
Appear right here if you go to view and then click right here on to terminal this will make it appear down here however if we now execute this file by entering – and now the file name so game dot py like this and it enter you can
See that we don’t get an error or something like that but that nothing is printed or displayed down here now by saying printed I already gave you a hint what we can do next because in Python we can print or display the output of our code so to say by adding so-called print
Statements now a print statement is written like that you type print then you open the quotation marks right here and then you enter well basically what you want to print right here this could be a string for example like Hello if I now repeat the command right here like
This you can see that we printed hello down there now in our case we don’t want to print any random string we might want to print the information that we stored in a variable for example right here in our player name for that we remove the content here and now add player name
Right here as you can see we already get these suggestions in the IDE right here so this makes it easier to select the variable we want to refer to him in that case I will use player name and hit tab and if we now save that and run the code
Again you can see that manual down here is printed in our console so these are two important things we learn already we can use variables and we can print the output of our code in the terminal by using such a print statement now the problem is that actually using for
Variables for our player is not really the most logical approach actually wouldn’t it be better to basically have one variable player for example and if we would then have an option to access a specific information of that variable for example the attack right because if I want to do this now I have
To change player name to player attack right here and then we run the code and this will return the value for the attack but as you can see that’s not really convenient and also not a really nice coding style right here therefore we need an option to basically
Combine all this information in such a single variable for that let’s maybe create another variable and let’s call it player this time and now set it equal to well to what and – we have different options to add multiple information to such a variable a very common one is a so called
List a list is always written like that so you use the square brackets right here and then you enter content into your list in our case our list could have four different types of content it could have D string Manual which will be the name and the four at the free
Integers 10 16 and 100 so I could now type manual right here then I could type 10 16 100 that would be possible and if I now comment out this code right here this is shift command + 7 right here as a shortcut that I can use and as you can
See by that we automatically add the hash tags right here but at the beginning so this means this code is now not used you could say so this code will not be executed basically it’s just there as kind of notes for us or we can keep the code right here and maybe reuse
It later but it’s not used at the moment and here we get an error now because of that because as you can see the variable is no longer defined because we commented it out but if I now change the player attack to player right here save the file always important otherwise
It won’t work and now run Python game again you can see that the content of our list right here is printed we can also check the type of the content we have right here because I’m referring to this list all the time and if you add
Type right here in front of the variable that you want to print and now add the the renfa’s is right here and if you’re now safe to file and run the code again you can see that we created a list right here that’s also an important concept
You can always check the type of the information you stored right here for example in your variable by using this type method right here however we don’t need that here we only need to print it and the problem is also that we don’t want you access the entire content as
Before we might want to access the name only right here we can do this with lists for that we simply add the square brackets again right here and now we can basically select a specific item in our list by using a so called index the index always starts with zero
In such a list what does this mean this basically so the first item has an index of zero the second item ten right here has an index of 1 and the third item has an index of two before if item has an index of 3 and so on
So this means if I now simply add 0 right here so the index of 0 right there and now run our code once again you can see that manual is printed right here in our console if you change it to 2 for example then what is printed well I hope
You guessed it it’s 16 now because we have the index to sew deferred item in our list right here so with that we now achieved a lot already we are now able to store all the information that is related to our player in such a list and
We can access it by simply using this player variable and then using the index to print a specific item in that list now besides accessing it we can also change the value that we have in such a list we can do this by simply typing player then again the square brackets
And now again the index that you might want to change let’s say we are not happy with our value for the attack right here let’s say we want to set it equal to 12 for example for that we access our current list item with the
Index 1 and now set it equal to 11 maybe like that if I now print there right here so we print the item with two index one again and if we not execute that code you can see that we change the content of this list item to 11 however let’s say we are
Happy with that and we don’t want to change the value because there is another problem right here although we stored the information in our list we don’t actually see what the actual well value is I mean it’s obvious for us that this is a name but how should we know
That 10 right here refers to an attack right here this refers to the amount we can heal ourselves and that this is our health bar that’s not totally clear right here therefore a list is not the perfect solution in this case so let’s also comment out this year because
Besides lists we also have another really common option to store data right here in Python these are so-called dictionaries a dictionary works like this you type player and now set it equal to content in curly braces between these curly braces you now don’t add the items as before so with like manual
Right here then a comma and then the next item it doesn’t work like that because dictionaries always work with so-called key value pairs that what’s a key what’s a value well in our case we store the name as a value so our key would be the name it’s type name right
Here it’s important in the quotation marks it is important but then we don’t add a comma or something like that we add a colon right here and then we add the value that belongs to that key in our case this is manual so the name
Like that then we add a comma and then we add the second key in our case this would be attack like that then we had a colon and then we add the value for our attack this is 10 in our case no quotation marks are required of course
Because we have an integer here after that we have a heal key this one right here so quotation marks colon and the value and finally we have our health at there : and 100 so this is now a so-called dictionary as you saw dictionaries work different than lists
And this also refers to the way we can access our items in the dictionary if we for example run our code once again then you see that we get a key error right here so we basically can’t access our dictionary with this index right here this doesn’t work the reason for that is
That we need to access the values that we stored in our dictionary by the key so in our case we can simply say that if we want to access the player name right here and they finna run the code like this you can see that we print the name
Manual right here once again we can also access different values of course by simply changing the key this could be something like attack ready for example if we now run the code we can access the value of 10 now such a dictionary is of course a very better approach to access
The information we have in the dictionary because throughout the game we will have to access our attack we will then have to access the impact that the attack has on the health of the monster and so on so with this dictionary we actually got a nice way of
Storing our player and monster related information now because of that we can delete all that out commented code right here like that and now we can also add our monster right here it follows the same logic actually so basically our monster is equal to a dictionary and the
Monster will also have a name key like that and let’s give it a name of maybe max for example then we have an attack also for the monster which could have a value of maybe let’s say it’s a bit stronger 12 we might have to adjust these values
Later so let’s use these as dummy values for the moment then we have well we don’t have a heal option because it’s a the monster can only attack the player and can’t heal itself and this means we only need the health right here at the end with a value starting value of 100
Also now we got the information we need for the monster and the player but so far the user doesn’t see anything right here on the screen you can see the different values depending on the key we access but actually that’s not information that we want to display when
The game started what we actually would like to have is some kind of user interface which guides the user to select a specific option in the case of the player this can only be attack or heal these are the two options we have but we can use the print statements we
Learn already – well create such a user interface let’s say we first want to print something like please select action we learned that we can refer to variables and to print the information stored in the variables already but of course if we add a single quotation mark
As I showed you some minutes ago and add a string right here then we can also print this information so as I said we can say please select action like that and then we can add two more print statements with the first one saying something like attack right and the
Second option would be to heal player or just heal maybe like that if we save that and run our code once again then you can see that these statements are printed right here in our terminal and this should now be the starting screen the player sees when he starts
The game the problem now is that well we basically can’t interact with that as you can see we don’t have any options to we’ll add information right here or something like that so we basically only have these print statements therefore we need to allow our player to add some
Input to our game right here and this brings us to another default method pipe with the input method the input method is simply well written like that input and now by adding the parentheses right here and that’s it already with that if we save that now run the code again you
Can see down here that we now have the option to enter some well information material to add some input this could be 1 2 3 whatever in our case we should only be able to select 1 or 2 also something we’ll work on later but if I
Now add 1 right here at the input and then we exit the game already now this exiting problem is something we will also have a look at in a few minutes for the moment let’s make sure that something happens after the player selected a specific input for that we
Need a so called control structure in Python control structures are for loops while loops or that’s one we need right here if statements an if statement simply allows you to execute a specific code that you define of course if a certain condition or multiple conditions are true now let’s create such an if
Statement to see how this works in our case to create an if statement you simply type if and now you add the condition that should be true to make sure that the code you then define is running in our case the input right here so basically the information we returned
Right here should be equal to 1 right so if the player entered run in this should then lead to an attack against them against the monster we are gonna need to declare a variable right here and this variable now could be player choice for example
Like that with that we can now refer to our player choice right here and if this player choice is equal now important not with a single equal sign because this will basically set the variable equal to well something in our case we don’t want to change the information stored in the
Variable we only want to check if the information stored in the variable is equal to something this means we have to add a second equal sign and now say if our player choice is equal to one then we have to add a colon and then if you
Would enter you can see that we get this automatic indentation right here this is important in Python we basically use indentation to specify where our code belongs to in our case this means that this code right here which for example could be a print statement saying attack
Right here only gets executed if this condition is true so if this is true execute the code this indented code right here if we run our code now you can see if we get one attack well we don’t get the print statement right here right so this doesn’t work here we only
Get one this was our input but nothing is displayed and actually our player choice was equal to one but this is not working the problem here is that input right here always returns a string and this means we compare player choice right here to an integer now player
Choice is not equal to this integer to you to one therefore this code right here is not executed again you can also check this by adding type um player choice like that and if you now say that you print this statement first right here then you can see if you had one
That will return a string right here that’s what I was referring to therefore this check doesn’t work at the moment now let’s get rid of that code and let’s now simply turn this one right here into a string by adding single quotation marks like that if we do this and vulnera once again
Add it attack then you can see that attack is printed right here and with that we now wrote our first successful if statement right here now with the new knowledge we can of course also add the second condition we have right here if the player selects you we want to heal
Our player for that we simply enter additional code but now important not indented because as I said the indented code will run if this condition right here this one is true therefore we have to start right here and now add a so-called Elif statement Elif simply
Stands for else if so basically if this condition is true do this else so Elif another condition is true then well execute another code now this other condition right here is pretty straightforward for us of course we simply have to check if our player choice is equal important single
Quotation mark to and if this is true then we want to print let’s say he’ll player like that and we could also say that if our user enters any different input so not one or two then something like invalid input should be printed to our console to our terminal down here so
With that again not indented we add else right here this time else is different from Elif as else doesn’t require any additional condition it simply says well if this is not true and well if that is not true well then in all other cases simply execute a different code in our
Case this could be as I said print invalid oops invalid input right here like that let’s now run this code so again so if we enter one we can see that we print attack if you run the code again and two we run he’ll player and if we enter
Anything else like five for example we get the output invalid input here now with that we got our first logic in here let’s now focus on to our first condition right here so if the player shows is equal to one because these print statements are nice but actually that’s not
What should happen here because if the player choice was one then we want to attack the monster this means we want to access the attack key right here in our player with the value 10 and after that we want to access our health key right here in our monster variable and
Decrease the monster health by this 10 right here or whatever value we use for our attack right here now how can we do this well let’s maybe space here and let’s now delete that print statement because we learned everything we need to achieve this the first thing we need to
Do is we need to access our monster health because that should change in the end for that we can again refer to this monster variable right here and now add the square brackets right here and access our health key we learned that already so we simply type health right
Here this health should now be equal to something else this means we don’t want to compare it to anything we want to set it equal to something so we want to change the information stored in that health key for that we use a single equal sign and now say that again the
Monster health right here so important I’m not referring to the initial status of the monster health so this 100 right here so this initial value for the monster health should decrease by well by what well minus the player right our player variable again the square brackets and now attack so basically by
The value that is stored in our attack key now after that let’s say we want to print our monster health once again to see if this operation was successful for that we run the game once again we now attack the monster and as you can see apparently it worked because now the
Monster health decreased to 90 so this is pretty cool but actually the monster is not the only party which can be attacked right here the player can also be attacked now we can use the same logic actually we have right here let’s maybe add the space down here and
Let’s now say that after the monster was attacked by the player well the monster will fight back so also the player health will decrease now for that we can use the exact same logic we can simply say that our player health right here is equal to our player health so the
Initial value we have for the player health – our well monster attack right here like that and let’s know maybe also add the player health to our terminal down there to see if it was successful and let’s then check what happens so if we run the game once again the player
Attacks the monster and the monster fights back this means after this round the monster health is at 90 which makes sense we had an attack of 10 against this health of 100 and the player health is at 88 because the monster attack of 12 well had an impact to our initial
Health level of 100 so if you run the game once again and attack again well we can see that we got the same value and the reason for that is that we always exit our game after the first attack was done or the first two attacks one for
The player one but a monster we’re done because what – at the moment does is it basically scans our code from top to bottom Sisu k we have a player variable we have a month’s variable we print this code right here then we have an input and
Then we have this if statement right here so if we select one Python symbol you see is okay we selected one so I will run this code I will print monster and player and then I will stop executing the code because we are done there is nothing to tell – please repeat
The code or anything like that now this brings us to the next control structure we need right here we need a way to tell python that it shouldn’t exit the game after first attack was done but it should continue the game until a certain exit condition that we define and exit
Condition if you remember back the slide would be something like the health of either the monster or the player is equal to or smaller than zero this is something we’ll implement in a few minutes let’s focus on to this logic to stay in the game or to keep executing
The code until we tell – that we are done and that we want to exit the game now we learned about the if statement already which allows us to create this structure right here another control structure is a so called Y loop a code running inside of such a while loop is
Executed as long as a condition that we define is true now this sounds kind of abstract so let’s create this Y loop now and then see why this is exactly what we might need right here for that we will add the new code up here because when we
Initialize our game this information is important for – it has to know the initial values for all variables but once this initialization was done we only want to execute code that is running inside of our while loop as long as this while loop is true our game is
Running if we exit the while loop well the game is no longer running so we create a while loop by simply typing while right here and now we have to define a condition and in our case the condition could actually be something like well while our game is running well
Execute that code as soon as our game is no longer running well then don’t execute this code again this means we can actually declare another variable up here before we create a while loop and this could be something like game running equals well true now what is
True now true is a so-called boolean boolean x’ can be either true or false I showed you that in the beginning already these allow us to easily control while loops for example because if we say that the initial value for game running is true well we can simply add
This logic right here to say game running equals true so basically as long as you don’t change the value right here so it’s true here well please keep it running but if we want to exit this loop now we only have to change game running from true to false and with that this
Condition is no longer met and we exit our while loop immediately important though to be able to tell Pyfrom that our code should only be executed inside our while loop or that the code belongs to this while loop basically we have to indent the code this means we can select everything
Right here so all the code we wrote so far and now it tap to basically indent it now if you now save this we shouldn’t get an error and as you can see if we now run the code – game attack attack we stay inside our loop and important the
Health values also decrease as you can see the monster has help of 18 and the player of 76 which makes sense after two rounds because the monster received two attacks 10 each from player and the player received two attacks from the monster with 12 each so 99 – sorry 80
Versus 76 and if you continue attacking this decreases more and more we can also get to health levels below zero as I said this is something we’ll address soon the problem is that we don’t exit our loop till now for that as I said we only need to refine a case where this
True changes to false for that we could add another if statement so after these statements right here if our player health right here is smaller than or equal to zero well then we simply say that game running should be set to false like that if we now exit our game we can
Do this by hitting ctrl + Z right here no here this may be and now run the code once again we can attack attack again again again again again again oops sorry invalid input so again again and let’s see yep now we can see that we
Reached a health level of -8 for our player and because of that we exited our game why did this happen just repeat that we said that the game running variable is set to false as soon as our player is that basically this is the sign for our while loop to
Stop being executed and with that we’re basically outside of our loop well and with that we basically are outside of our code because there is no more code to run at the moment for Python now by the way we used this double equal sign right here right these two we also used
This right here this smaller than or equal sign these are so-called comparison operators in Python we saw two of these already we also have more we also have something like smaller than larger than larger than or equal to and we also got this this one right here
Which basically means not equal to so these are operators we can use to basically check if a specific value is equal to another value so as you can see right here these are really helpful besides these comparison operators let me change the back to smaller than and
Equal to we also have another really helpful operator type in – these so called logical operators and these can be helpful right now because at the moment we only check if our player health is smaller than or equal to zero but actually if our months is that we
Also want to stop executing our code and geological operators are simply and so this means it would check if the player is that and if our monster health right here it’s also smaller than or equal to zero but that’s basically not what you want right here because you basically
Want to exit the game if either of the two is dead and not a faux fur that we could also use not right here which would simply check if the player health if the player is dead and if the monster is not that so if the monster is still
Alive also not 51 right here and we can also use or another logical operator which simply says check if either this condition right here this one or that condition is true and if that’s the case please execute the code right here now if we quickly change our player attack right here to allow
Above the one from the monster 213 for example and run the code once again you can see that D monster health the first one right here it’s at 87 and so on this and like this and like that and now as you can see our monster is dead appear
And we stop our code execution so the logical operator right here also pretty helpful if you have situations like that or you want to run a code depending on whether one of multiple conditions is met however our current code has a logical issue because we do check if the
Player or the monster is that down here but wouldn’t it make more sense to actually check this after each attack because let’s assume that our monster health decreased below zero after the first attack our player made this can happen right here right so it is possible that this code runs then the
Monster has a health level below zero but what will happen then is that the player can still be attacked by the monster we don’t have any logic to check if the monster is able to make his attack because well it’s only checked after this if condition was one so after
We entered the command we make our two attacks one by the player one more monster and then we check if the game can continue running and this doesn’t make a lot of sense actually so adding the check actually well right here after the monster attack and right there after
The player attack is what we should do next for that we can use our new knowledge that we gained regarding boolean’s right here because why don’t we use something like a player one boolean and a monster one boolean and then we add an if condition right here to check if the
Monster is still alive so basically we do something like if monster health right here is smaller than or equal to zero then we have to do something we basically have to end the game we’ll implement that code soon but for a moment let’s simply create this
Placeholder and to do this we can use pass right here so what pass simply does is it basically shows Python okay there is some code required right here at the stage I did not create that code at the moment so Python simply sees okay we need some code right here but it
Doesn’t throw an error it basically understands that we have a placeholder right here you could say and therefore it keeps executing the following code without an error because if you would not add this right here you would get an error and if that is not true so if the
Monster is alive well then the monster can attack back so we can add else right here no Elif statement required because we basically need to say only okay the monster is alive so let’s attack the player so else we say the player health should decrease and afterwards we of
Course also have to check if the player health right here is smaller than or equal to zero like that and we can also add pass for the moment because we will add the code in a few seconds these print statements can stay here for the moment we’ll also put them to a
Different position later now these are the if checks I talked about but I also mentioned something like player 1 or monster 1 these follow the same logic we have appear for game running the only difference is of course that we will basically declare that variable inside
Our game because we don’t need to add these right here because the information if the player 1 or not is not required outside our actual game logic right here but it’s part of the actual game so let’s say as long as the game is running the initial value for player 1 should be
Equal to false because when the game starts or as long as the game is running nobody won and the same thing is true for our monster 1 variable right here this should also be set to false like that now with these two variables defined we can
Our quadrant here and say that if our monster is dead well then player one right here should be set to true because well the monster died player one and the same logic also applies right here if the player is dead if the player is that then our monster
One should be set to true also right because then we can use this logic right here and go down to this if check and basically change this logic to simply player one equals true or monster one equals true right like that well then the game ends so let’s see if this works still
Let’s run the code that’s tak it’s attack let’s attack or attack like that and now if we hit one once again we can see that the game was accident because our player won the game as you can see right here the monster only has a health
Of minus 4 so it’s basically that so with that we refactored our code we now have these checks at the right position another thing that I don’t like though is that we basically exit the game as soon as the player or the monster won basically it’s correct that’s what we
Wanted to implement so I wanted to kind of escape this while loop right here because we say that the game stops as soon as somebody won but actually it shouldn’t be the entire game that stops after somebody won it should actually be only the current round that is stopped
Because what we actually need is we need to make sure that as soon as the player or the monster won the default values for the health so 100 for the monster and the player are set again and with that we should be able to start the next round immediately and not exiting the
Entire game and then having to enter pipe from game py again to start the game that’s not what we want but we can implement that easily now with our knowledge that we gained so far because why don’t we just add a second while loop which is something like new round
So as long as new round is true we keep running our current game as soon as new round is false so as soon as the monster or the player won we exit the current round and start a new game now let’s have a look at how we could implement
That because we could say right here after our first while loop we add this while loop so let’s say while new round is equal to true right this should happen so let’s maybe indent our code right here once again like that and we can define the new round right here
Inside our first while loop so new round should be equal to true at the beginning now with that we can say that as long as scheme running is set to true which we defined right here new round is also set true and as long as new round is set to
True this code should be executed this means if we simply change down here our game running right to new round right here we should be able to exit our round as soon as the monster or the player won and then we will go back to our game and
This means the logic that we exit the entire game as soon as somebody one should now be fixed now let’s see if you run Python game and click attack multiple times hmmm this is not working the code keeps getting executed and we don’t have any kind of hint that we actually exit this
Second loop right here the problem here is this part right here our variables because we never leave this first while loop we basically set game running to true then as long as this is true we say that neuron to be true then we say as long as this is the case please execute
This code then we set new round to false and what happens then is we go back to this first right loop this is nice but what is missing here is the resetting of the values so we basically continue using the values the changed values already and that’s why the health level
Right here keeps decreasing as you can see up here besides that we also have to issue that only the monster health keeps decreasing that’s simply due to the fact that we never get to execute this code right here because what’s basically happening is at the moment that we have the
Situation that the monster health is smaller than or equal to zero well and this means the code rodriram is no longer executed so we’ve each round we start again our while loop then we come right here to if monster health is smaller than or equal to zero well this is the case
Therefore we go down here to if player one is true or monster one is true in our case the player has won something you run this false and so on so we basically skip this second else block right here this will be automatically fixed if we implement the logic
Correctly now now as I said the problem is that we don’t reset the values and to easily fix that we can simply take this code right here and put it into our first while loop right here with the correct indentation of course with that we make sure that as soon as we leave
Our while loop right here we go back to the game running and initialize our variables with these default values now let’s see if this works now if we now run the code hit attack multiple times to see that the monster should be dead now now it is not it’s attack
And as you can see now the monster is back at 87 after first round this means the player made one attack if I continue doing this you can see now the monster is dead – for now we have another round and the values are reset it like that and the monster again has
Only a health of 87 so with the new structure we basically control the entire game logic only with these two while loops so this one right here and this one right there the only problem we have now is that we are not able to exit
The game but this should be an easy fix now right because we can simply add a third print statement right here so print free exit game like that and now we can simply add another Elif statement right here so Elif player choice is equal to 3 then we say oops
Not like that at the rotation mark right here then we can say that well what should happen well game running right here should be set to false with that we will also exit this value so let’s say game running should be equal to false right here now let’s check that let’s
Leave the game clear it and run the code so attack it’s working and if I hit free now and exit the game hmm this is not working do you know why well of course we are setting game running two folds but we are still right
Here in our new round loop so we of course also have to change our new round to false with that if we run our code once again oops like that like that attack works exit game works now we are out of our game is
It again we can start it and exit it so this is also working fine now we are making a good progress with the game in general I would say I would like to work on two things now though the first thing is I would like to add another input field because at the
Moment we have a fixed value for the name key for player and I think it would be nicer if we can add our name individually right here we could also change this for the monster but I think the fixed monster name is fine right here as a second step I would like to
Make our game a bit more beautiful so maybe add some a print statements and yeah maybe make it better display the health information more often things like that but let’s start with that additional input right here because when we start the game I would like to be
Able to enter the player name now to do that we can simply arm add another print method right here print function and this could be something like enter player name right here now with that we print that but of course we also want to be able to change the player name
According to our input now for that we need to access the name key once again and change the value so why don’t we just set player name right here equal to not sure what and input once again because the player should be able to enter the name on his own right here
With that if we start our game once again we can see that we can enter the player name let’s say menu right here and then the game starts and as we can see it arounds continue like that and as soon as the game ends yeah we can enter
The player name once again so this is working fine I would like to have some more visual improvements now as I said a few seconds ago the first thing we could do right here is we could add an additional print statement on top of our enter player name and this print
Statement should should just be some dotted line you could say which separates the content a bit makes it easier to read in the end for that we can simply print and now we enter two quotation marks and now we simply add three lines like that so free – is
Basically now we can use another arithmetic operator right here this one right here to start to multiply these by a number of our choice let’s maybe say 7 or something like that this simply means we want to print 21 of these dashes um before the enter player name text is
Printed so let’s look at that if we exit the game like this you can see this is this nice line right here and I think this improves the look of our game a bit we could also add such a well separator you could say right here before we can
Select the actual action this is just some cosmetic improvement of course nothing that is really required but I think it’s nice to freshen up the game a bit with these things feel free to add this add additional positions I’m happy with that at the moment what I’m not
Happy about this is the fact that we don’t CD health at the beginning of the game and also at the end of the game because if I select the action now you can see that we see the health right we see it right here but we don’t have any kind of
Information regarding while the player health is this value and the monster health is that value right here so let’s first add the health information at the beginning of a game and then improve the way the health status is displayed throughout the game now at the beginning
Of the game means that we have to go up here so once the game is started up so we can say that after the player entered his name we want to print right here our player name right here and now we want you at the information about something
Like the player so manual or whatever name we gave him has a 100 health like that but 100 health should be retrieved from the dictionary right here from the player and 100 health for the monster should be retrieved from this dictionary because we could also change our health
To 200 or something like that when the game starts so I want to make sure that this is displayed at the beginning and that the player has the information about the health amounts at the beginning of the game for that we have to combine different types of information right here the first thing
Is right here the value from the dictionary this will return a string so this should be fine right here but if we now add a plus sign like that we can combine a second information that should be printed right here now as I said if a
To print the player name has a specific amount of health then we could add something like has right here so an ordinary string we can also add space at the beginning and at the end because this will make sure that the player name and has it’s not connected directly but
That we have some space in between these two values that should be displayed right here after that we can add another + to add our player health right we learn how this works so the player health should be displayed like that so with that we can now access the health
Value of the health key with the corresponding value of 100 right here the problem though is that if we would execute it like this we will get an error let me show you this if I start the game once again and the name then
You can see that we get an error that we can only concatenate strings not integers the issue is that player name right here now print statement is a string it’s manual right here or if we change it it’s the input that the player gave us so menu in this case has isn’t
Is a string we can see it right here but player health is an integer and this causes a problem here because well as we said we cannot concatenate strings and integers there is one easy solution for that issue though we can simply use the so called string method in Python to
Turn our value right here into a string now how does this work we simply add STR in front of the value that should be kind of converted to a string you could say now we open the brackets right here and we are done because as you can see
Right here now we basically well kind of wrapped our player health into this string method and with that it should be turned into a string we’ll see in a few seconds if this works let’s just add one last item right here to our print statement and this simply is well
Another string with a space at the beginning health because like that health because I just want to print player name so whatever name we give the player has 100 so this value health let’s see if this works let’s save it and let’s run the code once again let’s
Enter menu and as you can see we print menu has 100 health right here on top now this works fine so let’s also do the same thing for our monster right here here we simply have to change the monster name right here like this and this should be the monster health
Important to keep in mind player name right here refers to the variable right there because we gave the variable a different information right here so we said that the player name is equal to our input the monster name right here is still equal to the value we have for the
Name key on top right here in all monster variable with that if we quickly exit our game run it once again and the name we see that we have menu with 100 health and Max with 100 health too we could also add to make it a bit more
Readable the print statement right here again so that we have the separation so if that we made the startup a lot more beautiful let’s not have a look at the content we print throughout the actual round while it’s running because right here ice is something that I don’t like
These to print methods right here because our if statement right here should actually just contain the logic to see how much health impact the player attack has and then to see if the counterparty saw the monster or a player right here is still alive of course we
Also want to make sure that we kind of show the impact of the monster attack but these to print methods no I don’t think this should be placed right here so let’s delete it and let’s not think about a better location for these methods well I think about a location
Would be down here because here we have the actual selections but after these selections are checked so after we know which choice the player made then we can add these statements and for that we should refactor this if statement right here – because what is actually happening
Well if player 1 right here is equal to false and now we use the end logical operator and if monster 1 right here is also false so basically this means both the player and the monster are still alive then please print something like player name has
Player health – same logic we used above already left but what do we have to do right here we have to put the left into quotation marks and data space and we have to combine these different values we want to output with a plus right here and right there
The same thing could be printed for our monster of course so we could say the monster right here also has a specific amount of health left by the way selecting these two items at once simply works by pressing command D on the Mac right here here we have an issue still
Because I overlooked this has so we should select both of these and now put it into sink rotation marks and add a space at the beginning and at the end of it now this looks better and we should add the plus right here of course like
This and like that now we don’t have an error here anymore but we should also be factored this code down here now because if nobody won well then this code will run but L if so else if the player won by the way we can also get rid of this
Truth statement right here because if you just write like player 1 then this automatically means that if player 1 is true so basically writing this player 1 equals 2 is the same thing as writing if player 1 so if player 1 we could say we also print something like um player name
Plus 1 and of course new round is set to false then that is to correct and another Elif right here if the monster 1 like that new round is also set to false and we should print right here oh maybe not refer to the player to monster name we
Could simply type something like the monster one like this right here now if that we refactor our code a bit we are now able to print different statements depending on whether the player has one or the monster has one and we also print the current health statues if nobody won
So if the round basically continues one thing we have to change your dough is we have to turn our health right here into a string again that’s also the same issue we had above but we also edit the print statement so let’s add a string method right here and right there like
This so if we now restart the game it here enter the name attack we can see maybe let’s crease a little bit we can see man us 88 left so it decreases it keeps decreasing and now we see that menu 1 because the game was over as we
Can see the monster so max only had 9 left and we see that I won the game in this case so this is working final so and with that we improved the structure of our game a lot as I would say a good next step now would be to also work on
The other if statements specifically I would like to work on the heal option right here because at the moment we can basically just attack or exit the game but heal player still is simply not just a print statement so let’s work on that now if our player says that he just
Wants to heal himself and not attack the monster then the logic is a bit different because what will happen then well we can simply say that if the player wants to heal himself then player health right here so we again access the health key of our player is a quote you
Well the player health that he currently has but now not minus something because we don’t want to subtract something we want to add right here the heal amount we defined for our player just to bring that back to your mind the heal amount is this key right here so we have a heal
Key with a value of 16 so let’s add this now so we can say that playerhealth is equal to the current playerhealth plus the player heel that we have right here what we don’t need here is now we don’t need to check whether our player is still alive
Because well if we survived if the previous round then you will be able to heal himself because of that the health will increase but after the player healed himself well the monster will still be able to attack the player so we have to add this monster attack now to
Do that we can simply say that player health now but this is now the updated player health so this one right here so player health is equal to well the player health again so the current value for the player health – the monster attack that we have so basically it’s
The same logic that we had right here however after this attack we have to check whether our players still alive so we can again copy that code right here so if the player health is below 0 or equal to 0 then well unfortunately our player has lost the game and the monster
Has won so this would be the logic for our second choice right here for our second option let’s quickly check if this works let me enter name and exit the game so let’s now we start the game and enter name once again and let’s attack yeah we have 88 life left so
Let’s see if we kill ourselves we got 92 left which basically makes sense because we got an attack from the monster of 12 but we had a heal level of 16 so we increased our health by 4 so this is also working fine now with that
Code added I think it’s time to think about the core game logic once again because at the moment we have a pretty well let’s say easy to predict game result because we only have two attack options for the player and monster so it’s 13 for player and 12 for the
Monster so basically if the player always attacks well he will win the game that’s pretty boring so I think we should make this a bit more dynamic and I actually don’t want to change the player values right here I think these are okay but I’m not happy with the
Monster right here because wouldn’t it be great if we could make this a bit more dynamic if we define a minimum value for the monster attack and a maximum value and then kind of create something which allows us to dynamically select the value in between this minimum
And maximum with that we have a random element in our game and this would make it a lot more exciting now how can we achieve that well the first thing is we should get rid of that single key right here and add two keys instead of this
One one could be attack min right here for the minimum attack value which could be equal to let’s say 12 and then we add another value or another key and value which could be attack max right here which could have a value of well let’s
Say 20 maybe even this can make it a little bit more exciting let’s maybe decrease this a bit to 10 let’s say something like that with that we now have these two options the problem is that we now need a way to well kind of choose a value in between 10 and 20
Randomly for each round for that purpose we need to import a so called module chip – now what is a module generally we could write everything from scratch right here in Python this is generally possible but isn’t really convenient and it would mean that we have to write a
Lot of code and a lot of logic just for things like while creating such a random number in the end because of that – comes with a lot of different modules which can be imported to your project and which come with these features implemented by default but the important
Thing is that you can use different features of this module in a very very convenient way now let’s see how this works now because I think then it will become a lot clearer you basically import a module to – by simply going to the top of your code right here and then
Import the module of your choice in our case we need to import from the so-called random jool more about modules can be found in the link in the video description by the way and from this random module we want to import a specific feature you could
Say in our case we want to import the rant int feature this one right here the rant in feature simply allows us to return a random integer between a bottom and a top corridor you could say so in our case between the minimum attack and the maximum attack and the cool thing
About this module now is that we can simply use this feature without writing any additional code we only have to use a specific method as I said methods and functions or something we’ll cover in a few seconds and then we will get back a random number now a lot of talking how
Does this work now well basically we want to have our monster attack down here so this basically this monster attack is now no longer a fixed value but this should be a random value for that we can simply define a variable let’s say monster attack really
Important this now is a variable you can see this with the underscore right here and I don’t refer to the monster attack key right here right that’s important so I’m not referring to any kind of attack key which by the way is also not existing any longer and I want to store
An information a specific information in it variable and this information L is something that is only available because we imported this random module I will now use a so called function or a method and we will dive deeper into functions in a few minutes so just to give you a
Big picture a function is simply some code that can be executed whenever we call this function so whenever we tell Python to well refer to that function you could say then this code will be executed and the Rand int is a function which is available due to the random
Module be imported so we don’t see the actual code inside that function right here but we have this function or method available now as we use this random module a function is called by typing the function name and adding the parentheses right here so adding the parentheses simply means
That you call the function we have two types of functions though all to something we’ll have a look at later by the way we have functions with empty bar and parentheses this would be something like this or in the case of the rent in function we have a function which
Requires so-called parameters or arguments if a function requires such arguments this simply means that we have to pass some additional information from outside to this function to make sure the code inside the functions can be executed correctly and the information we pass to this function our so-called
Arguments we will have a look at parameters and arguments also later in more detail so the important thing right here is we have this rent in function so this predefined code we can execute whenever we need it and we have to add additional information to this function
To make sure the code can be executed correctly now as we want to calculate a random integer we need to add two arguments here the first one is the minimum value for our attack so we need monster attack min right here and the second argument is the monster attack
Max that’s the second argument and with that we are now able to call this function inside our variable and with that to retrieve the calculated random number for the monster attack with these two arguments added we now made sure that if we refer to the monster attack variable this function is called and
Executed and therefore we only have to change our monster attack right here with D monster underscore attack variable because if we now subtract this monster attack variable we will subtract the result of the Rand int function and this will be this random attack value calculated due
To our random module so this was a lot of talking let’s now see if this works so let’s exit the game and start it once again and player name and let’s attack the monster so we see menu has 83 left now so we kind of deducted 17 let’s try
It another time now we have 65 left so 83 minus 1818 is 65 so another random number now we have 50 left so 15 so basically this seems to work now if this random module right here we are able to create this random integer now we simply have
To also copy that code from here to down there to our second choice if we decide to heal ourselves and let’s make sure the indentation is correct right here like this and like that so this is not working fine but we just saw something that you should avoid no matter if
You’re writing Python code or any other code we just copy and paste it well basically the same code right that’s not always a totally bad thing but especially if we have things like this monster attack right here so this line right here and right there this is not really a good
Well code structure I would say because what is actually happening right here is we only need this code to get this random integer now wouldn’t it be better to kind of outsource this code and to kind of to be able to refer to the result of this calculation right here in
Monster attack at the moment this is not possible because we first have to define or declare the variable to get back this value situations like that are the perfect area or application area for so-called functions now what are functions functions basically allow us to run a specific predefined code
Whenever we call such a function calling a function simply means that we tell the function that it should be executed now now how do we write such functions let’s go down to the code and create such a function a function in Python is created by simply typing the death keyword right
Here now we have to give the function a name and the name should basically describe what is well basically done inside the function so in our case what are we calculating and this could be something like monster attack maybe like this the problem is that we have monster
Attack already right here for our variable and this is something you should avoid you should not name variables and functions equal that’s something you should not do they have or we will just change the name here to monster attack or maybe let’s call it calculate armed monster attack like that
With that we have a different naming right here then we add parentheses right there and then we add a colon and here we now have a function which doesn’t require any arguments you remember when we used to rent in function before we had to add the attack min and attack maximum as
Arguments otherwise the function wouldn’t work correctly in this case we don’t need that arguments because the function doesn’t require any additional information from outside and with that we’re basically back to our already known Python programming syntax because now we hit enter and now the indented code right here will only be executed if
We call so if we basically execute the function right here now what you want to do is if we refer to this function we just want to have this random integer so we can just refer to this code right here copy it and paste it right there
Into our function so let’s now see what happens if we save that right here we can see it we get no errors so this seems to work ok so let’s exit the game and let’s now start the game once again and rename attack well this works fine
Of course but the problem is that we basically don’t use the function right now because as I said before we have to call a function to really execute the code that we saved right here what does this mean for us well we basically should get rid right here in our first
If statement of the monster attack a variable we don’t need this variable anymore because the only purpose of this variable was to basically calculate this random number this is now done by our function and we can instead of that now say that we just refer to the calculate
Monster attack function and important to call a function in Python we have to add the parentheses right here let’s now save this and let’s now see what is happening so if we know X the game and her name menu like that say attack then we get an error it says that
The calculate monster attack right here this name is not defined but that’s strange right because actually we defined it down there but somehow puffin doesn’t know that this function exists and therefore has no idea what it should do right there now this is also very important to
Understand the code right here in Python and generally in programming actually is always read or parsed from top to bottom this means if we think about what is pi for doing right here it starts reading the code up there scans the code scans the code scans the code and then it sees
Okay we want to execute or call a function right here but the problem is that Python has no idea that this function is defined down there because as I said the code is parsed from top to bottom therefore how should it know that therefore we will take this function l
Scroll to the top of our code and add it right here ahead of our first Y loop with that pipe now understands that we have a function named calculate monster attack and that this function will be called right here now let’s see what happens now if you run the game once
Again enter the player name menu right here and attack now we have another problem Python is aware of our function but we basically have something like an unsupported operand type the coughing right here is this non type because our function is not returning anything at the moment because we basically told our
Function up here that it should calculate the random integer but the problem is we didn’t tell the function what it should do then so it calculates that integer but well then that’s it therefore we have to add another important statement to this function right here this so-called return
Statement like this return simply tells python that it should well return the result of this operation and the result of this operation is the calculated value for our attack so if we save that now and run the code once again enter the player name and attack then you can see
That this is working now because now basically what happens is we say that we call the function right here we calculate the value for our monster attack and we return that value and with that we insert the value right here in our function and therefore T playerhealth is recalculated what we can
Do now of course is we can also take that code so this playerhealth we just created and also replace it for our second if case right here important make sure to do the indentation indentation correctly too so besides the fact that we now refactor our code again made it a bit leaner
Which is always good we also had a first look at functions and to be more precise we actually solve two types of functions already let’s scroll up a bit to the functions we use because we have our calculate monster attack function and we have our rent in red here so the
Function we use from our random module that we imported and if you look at the parenthesis at the end of these two functions we see a big difference the first function right here has empty parentheses this means this function doesn’t require any arguments or parameters the rent in function on
The other hand requires two arguments in our case monster attack min and monster attack max what’s the difference here well the calculate monster attack function doesn’t need any kind of external information to run correctly because if we call that function we simply execute this code right here and
This code comes with all the information needed well to run the function this means we don’t need any parameters right here we don’t have to pass any arguments to that function so any external information you could say this is different for our random integer function right here we need to calculate
A random integer between a minimum and a maximum value in this minimum and maximum value in our case is some external information and to pass this external information into our function we use a so called argument in our case we have two arguments we have the minimum attack and
De maximum attack we don’t have to use these external information as arguments here by the way we could also use fixed values for example 10 as the first argument and for the max value as the second argument we could use 20 that would also be possible 20 like that this
Would also be possible but this would make our code a lot less dynamic because actually the monster variable right here and the corresponing dictionary contains information about the minimum and maximum attack so instead of using fixed values as arguments right here we can simply pass attack min and attack max as
Arguments for the first parameter so this is monster attack min and the second parameter receives the monster attack max as an argument so that’s the core idea behind functions parameters and arguments parameters simply allow us to add additional information to functions to make sure the code inside the function works correctly and the
Information we pass to these parameters are our so called arguments so attackman and attack max in our case however using the function like this is working fine in this case but this is not a perfect solution to be honest because at the moment we declare our variable some
Monster right here and we refer to this variable inside our function and after we declare the variable we call the function down here calculate monster attack but there can be situations where this can cause problems because what if we want to call our function before we declare the actual variable for example
We could print our calculate monster attack function right here if we run our code now we get an error because the named monster is not defined because what’s happening is that well – parses the code it reads the code and finds the function then goes into our
While loop and then we want to print the function and then we have a problem because – is no idea that this monster variable exists because we use it in the function but we declare it after we call the function therefore this approach is not
Perfect here we can stick to it for the moment though but actually we should improve that code to make sure we avoid errors like that however we will work on that as soon as we were diving deeper into parameters and arguments because an argument might be helpful here nevertheless if we
Delete it right here and run the code again we can see this still works fine so we’ll stick to that solution for the moment now why don’t we practice that a bit and create another function which also requires an argument well for that we can scroll down and here in our game
Ending or around ending situations where either the player one or the monster one I think it would be a nice place to add such a function because we could actually create a function that might be called something like arm game ends something like this which gets called if
The player or the monster one so we can actually delete this print statement right here and also write there and add game ends right here like this as you can see our 30 parentheses so like this we call this function and we don’t pass any argument we of course also have to
Define the function trait the function so let’s go up here again now we’ll create the game and function without any arguments and say that this should print something like maybe um player one like this if we do that and run the code let’s enter player name attack attack
Attack attack attack it seems that we might lose let’s see ah we made it though we actually won the game but the problem still is that even if the monster would have won the game we would always print the player one because we always call this game and function in
This game and function well it simply prints the player one so wouldn’t it be better if we would be able to pass an hour human to this function which basically simply is the player name in case the player won right here or something like the monster in case the monster won for
That we need to add a parameter to our function this can be done up here in the parenthesis and now we can specify a name for this parameter in our case this could be like the winner’s name for example like this so we now have this function with this parameter and now we
Need an argument that gets passed to that function to be able to use it inside our function at the moment we don’t use it but we will add that in a few seconds for that we can go back right here and now simply add the argument so the argument that gets
Passed to this parameter inside the parentheses right here now what is the argument we need in case the player won well it’s of course the player name right here like that and for the monster well I think that’s quite obvious it’s the monster name of course this and like
That so in case the player wins the game we call the game and function and we pass the player name as an argument this argument now gets passed to this function right here as the spinner name parameter important as you can see the naming of the argument right here and
The parameter right there doesn’t have to be equal can be but it doesn’t have to but important if you want to use the argument then inside your function then you have to name it identically for example if you want to print the winner’s name we have to add winner’s
Name right here like this with that if we exit our game clear it and run it once again and the name menu and attack attack attack attack attack attack attack we can see max right here so this means the monster because this is the name of the monster won the game
So this is working we now pass the argument to our function and can dynamically output the winner name the problem is that I might also want to have something like a little bit of a sentence right here because just printing the name like this it’s not really nice as I would say for
That something like using a string as we had it before for example player won the game like that would be good but player should actually not be the player it should be either to player name or if the player won or the monster name if the monster
Won and we have to use it dynamically well based on the argument we have right here to do this we have a really nice feature in – also it’s called formatted string literals or f strings and these allow us to dynamically inject content into strings so exactly what we want to
Achieve right here to create such an F string you simply add well an F in front of your string so this is the string that we are using right here and you simply wrap the content that you want to dynamically inject into your string with curly braces so like this and like that
This would be the player then being injected dynamically but as I said this is not working at the moment because the name of the argument right here and right there inside our function so where we want to use that argument has to be equal so with that we can simply add
Winner name right here so what is happening now is that if the player for example wins the game we pass the player name to our function and then we use this argument right here inside our f string if the monster wins the same thing happens but the winner name will
Be the monster name and not the player name now let’s see if this works so let’s escape our game clear it run it once again enter name attack attack attack attack attack attack and Tech max won the game so this is working fine let’s not also see if we can win
The game this so let’s maybe start repealing ourselves healing again attacking the monster healing ourselves ah this doesn’t work good healing can we win the game we might have to readjust the gaming logic a little bit I think we will lose again yeah now we won the game
We did it so as you can see now we won the game and this is also printed now dynamically because now menu so the player name is used in our function let’s move away from functions now and let’s work again on the user experience because we have this output right here
So monster or player on the game this is nice but actually I would like to add some more information on the player after a round or a game ended because let’s say that the monster won then we would have max won the game right here
But then I would also like to print the player info like the player named the player healthy had and also maybe something like how many rounds it took a single game to end so to either make sure to play a one or the monster one so for that we need to print this
Information and we need to add a counter let’s work on it step by step and let’s print the information first for that I will go down right here to our game ending conditions so either the player or the monster run we have our game and function and after that I would like to
Create a new variable this variable could be something like round result right here and the round result should contain the following information it should contain the name right so the name of our player and because of that you have to refer to our player name right here and it should contain the
Health level of our player as soon as the round ended and the health level is of course our player health right here now we add a print statement right here to print our round result and then we copy this information down here to our monster like that if you remember back I
Said that copying and pasting code is not always a best practice for this purpose here I will copy paste the code though because in a few minutes we will basically save our results and for that I prefer this approach I come back to that in a few minutes though so let’s
Keep it this way for the moment if we now restart our game enter the name and attack attack attack attack attack attack you can see that menu won the game so basically play up on the game and then we save to player stats or the name and
The health this can be helpful in case we have different players then each player can see his name and the health level he had now besides that I also wanted to add a counter so that you can see how many rounds it took until the game ended for that we simply have to
Define such a counter first and where should we do that well right here in our first while loop in our game running while loop because as soon as we start a new game not a new round a new game the counter has to be reset well and what
Should be the value for a counter nationally initially well it should be zero so we can say the counter is zero right here at the start of the game now whenever we start a new round the counter should increase by one because we start a new round the counter goes up the round
Ended the next round starts and the counter increases because of that we can simply add the counter right here in our second while loop so the new round while loop and we can again refer to our counter variable and say that the counter should be the initial value of
The counter this one plus one because we increase the counter by one and as long as we stay inside our round the counter will be one when we start the round then we go for our while loop we stay around the second round starts sort of counter
Increases to do if the round ends we go back to our first while loop right here and set the counter back to zero so that should be the logic let’s now also print this counter right here in our round result and for that we can simply add it as another key right
Here so this could be the round key right here and while the value for rounds is simply our counter variable let’s also add this down here um for our monster one case now let’s see if this works let’s end the game and restart it enter a name tag attack attack attack
Attack attack and yeah as we can see max won the game so the monster won the game the player was menu the health level was minus seven so we were beaten and it took the monster seven rounds to win against us so if that we also implemented such a counter functionality
To our game right here let’s now also try another player name so let’s say the other players Tony so another player Tony attacks attacks attacks attacks attacks attacks and again max won the game but the problem is that now Tony is only displayed right here but actually
In the previous round I was playing against a monster but my stats were not safe now keep in mind that we didn’t exit the game so we actually stayed inside our game but somehow our results were not saved this could be the next film we work on how
Can we save the data of a single round right here in our code to make sure we get kind of a while results list in the end to see how different players performed against the monster we actually learned almost everything we need for that already in this video
Because if you remember back the beginning of this video we talked about lists and lists allowed us to store well different items and actually this right here so this dictionary is a single item in the next round if another player plays against the monster the result would also be a dictionary which could
Be the next list item and so on so what we actually only meet is an empty list at the beginning of the game and to this empty list we then simply add the dictionary so this one right here every time a game was finished so every time
Either the monster or the player won for that we will first create this empty list and let’s create that list appear outside of our while loop because basically that’s the initial thing that should be created as soon as we run our Python file right here so let’s maybe
Call this armed game results and they should be simply an empty list so with these square brackets right here and well this empty list won’t help us a lot but if you now scroll down again right here to our well game ending condition as I was referring to him then actually this
Round result variable is everything we need right here we have this round result variable which contains the dictionary I was referring to so if we could just add this dictionary to our list we are done already and this can be done with a default method available for
Lists so let’s get rid of our print statement right here and let’s now refer to our list so we call it game results and now we use a special method by adding a dots this shows us all the methods or functions available for lists and the method I’m referring to right
Here is the one on top it is append because it simply allows us to append an item to an existing list and what do we want to append right here well we want to append our round results of course like that not round results the round result sorry for that like
This and we can of course also use this right here for our monster one case like that so with that we should be able to store the result of a round in our list then to start a new round with a different player name for example and as
Soon as this round ends we should also be able to add this result now so this new dictionary as the next list item to our list just to be sure we understand the code so let’s have a look we start with this empty list right here as soon
As we run our app – file then we start our game with our while loop right here and then we start our round we go through all our rounds so just as we did it before we know the code and as soon as either the player or the monster
Win the game we append the dictionary with the player results so in this case this dictionary or the dictionary down here to this existing empty list if this round was ended then we go back to our game while loop so this one right here but we don’t declare this variable
Once again because we declared the game results right here outside of our while loops this means we use our current lists of the game results list containing one item for the next round then the code starts again as soon as the round ends we add the new result to
This list and so on what will not work here is if we really end the game so if we use our exit game option right here then our list will not be stored so if you run the game once again then the result will be gone we could also change
That by saving the result to a file but this is something we won’t have a look at in this tutorial so let’s see if this logic I was referring to is working now before we do that though we have to make sure we arm print our result also so
Let’s print our game results right here and right there otherwise well we can’t see a lot listen like that so let’s now run the game let’s enter a name for the player let’s attack he’ll he’ll attack attack he’ll so I’ll play around a bit that looks good maybe we win this time
Yeah we can see that menu on the game we can see the health and the rounds let’s now add another player maybe Tony and let’s attack attack attack attack attack yeah and now we can see that we added the second dictionary also to our list
So this is the second stat and we can also do this with another player let’s say Sam quickly played a game and we can see Sam is also added right here to our list as you can see we have free list items and each list item right here is a
Dictionary so dictionary one or item one with the first dictionary item two with the second one and item free with the third dictionary and this is working nice but I’m not happy with two things now the first thing is that the result is printed automatically right here I think
It would be better if the results or the printing of the results would be another option so another player choice so we could have a fourth player choice something like right here print for our show results something like that and in addition to that I don’t want to show
The results like this so inside this list and also one after another I would like to have them below each other so we have the first dictionary right here then in the second line we have the second pictionary and so on so let’s first implement this fourth choice now
And lets them work on the way the information is displayed now as we know the logic already we know what we have to do right here we have to create another Elif statement right here there the player choice is for well what should happen then well basically we
Only want to print our game results so we can delete this print statement right here from our player one and maybe cut this one from our monster and now simply add this right here as the fourth choice like that that’s it already so let’s exit the game
And it started its player name if you know it for we should see nothing let’s grab a bit yeah here is our empty list so this is working fine let’s now attack and see what is happening then yeah now we see max won the game now we have to enter
The player name for the next round so let’s say Tony wants to start and Tony wants to see what the results from the previous players were so we can hit for now two shorter results and if we scroll up yeah we can see that the result of
The last round is printed right here so this is working fine this was the first step the second step now is to well change the way the results are shown as I said I would like to show the results not inside the list but dictionary by dictionary line-by-line for that we need
To have a look at the third control structure that is really important in – we had a look at if statements already we had a look at while loops which basically could basically control our entire application and now we have a look at for loops a for loop basically allows us to iterate
Well over a sequence a sequence is something like well a list or a dictionary in our case and to give back a result for each of these iterations now this sounds totally abstract of course therefore I would like to implement the for loop now step by step
Then see how it works and then also compare it to the while loop we created so far now where do we create such a for loop well right here in our fourth choice because what do I want to achieve right here as I said I would like to
Have the look at the content of our lists of our game results list right here and then I would like to basically print or display each single item off this list separately so if we have three items inside our list I don’t want to print the entire list
I would like to go for the first item print that I would like to go to the second item print it and then I would like to go to the third item print it and so on this is what I meant with iterating over a sequence over a list in
That case so basically what I want to tell Python is have a look at this list iterate through each item this list contains and printed separately that’s what I would like to achieve right here and for that we can use the for loop I was referring to you the for loop is
Written like this we add four and now we can add a name of your choice because what you’re now saying Python is basically for each item inside a list do something and for each item you can specify a name of your choice so for example you could say for item in game
Results like that so you say for each item you find in the game results list please do something at the moment we will just delete that and say we don’t do anything we just pass right here important item doesn’t have to be named item right here you could also name this
For score in game results or player stat in game results whatever this is simply what you name each single list item in your list so your can name this as you prefer the important thing here is that you say that for each item in this is also important you have to
Put this right here in the list in our case so in this game results variable please do something what you could do now is for example you can simply say that for each item you find inside our list please print and now important this item here the naming is important you
Have to name this right here equal to the name you gave to each item inside your list just repeat that what you’re not doing right here is we say please go for the list so the list saved in our game results variable right here and for each item which we named players that
Right here in this for loop please print me the corresponding item now let’s see if this works now so we can clear this now we can run the code once again and a name menu attack attack attack attack attack as always then we add Tony as the
Second player we also attack all the time maybe he’ll it once or twice like that and now we enter a third player maybe Sam once again and now we show the results if we scroll up a bit you can see that now we have two changes compared to the previous print statement
We had without the for loop the first thing is that the items are no longer stored in a list before we had the square brackets around the dictionaries because well the dictionaries purchased single list items additionally we also print the items line by line now because these are now single dictionaries
Because we simply access the dictionaries inside our lists what basically took them out of the list right here and printed them line by line so players dead right here so for players that is simply this dictionary right here and that dictionary right there so that’s the for loop and what’s
The difference now to the while loop that we previously made well here we iterate over the items inside our list as soon as the race is over well the for loop ends our Y loop simply keeps running as long as a certain condition is true so
If we never change the new round to false the new round will continue infinitely so we will never finish or exit this while loop this continues forever basically until the condition you define right here ends for the for loop this is different because the for loop will simply end as soon as the
Iteration already lists items in our case ends so as soon as the for loop looped through all the items well the for loop automatically ends in our case so that’s basically difference if you want to iterate few items use a for loop if you want to keep executing a code
Until a certain condition is no longer true then use a while loop so with that we are actually done with our game right here but now it’s the time to have a look at our code once again and see if we have some issues or if we can improve
Some things here specifically I want to work on one little problem you could say that we have right here I showed you that earlier in the video when I called the calculate monster attack function right here before we actually declare the monster variable if you remember back what we learned about parameters
And arguments then this is exactly what can be helpful here not what they mean by that why don’t we just say that we add two parameters right here to our calculate monster attack function for example attack min as the first parameter and attack max as the second
Parameter now we can copy them and paste them right here to our rent in function to be able to use them and now we only have to go down to our calculate monster attack function so these two functions right here and we can see the error already we didn’t add any arguments to
The function we can change that quickly because the first argument should be the monster attack min right so referring to our monster variable and the second one would be monster attack next like that let’s quickly copy these down here to the second monster attack so this is working now so
We shouldn’t have an issue but now if I again want to print our function before we declare the monster variable so calculate monster attack right here then we can add monster attack min and monster attack next once again so if we save that we get an error now right here
Because as you can see we are using our variable monster before we are actually assigning it this means if I cut the print statement now and put it below our variable we can see this error is gone and with that we improved our code a lot because now we can immediately see that
If we want to call the function to earlier well that we get this error and that we first have to declare the variable so with that this approach right here is the one to prefer because it makes sure that our code runs correctly and it helps us avoiding
Errors like that let’s delete the print statement though and let’s also get rid of that empty line and let’s now finally see if it’s the wroking so we run the game and for a name tag he’ll maybe crease it up a bit right here attack yep
This is working let’s say Tony wants to play again play yeah this works also let’s not say Sam wants to see the results yep this works fine and now we want to exit the game so with that we refactor our app and also finished our app for
This video I hope you got a good understanding now of the core basics of Python and especially how you can apply them in practice because learning to code in theory is nice but I think applying the knowledge and learning it by really practicing it in a practical
Project like this is a lot more helpful at least in my opinion so with that I wish you all the best for your Pyfrom projects dive deeper into Python it’s an awesome language it’s a lot of fun to work with and also make sure to have a look at academy.com
There you can also find more Python content and also a lot more content mainly related to programming and web development and with that well thanks a lot for watching and bye
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
A little helper for you hopefully:
———-
Install Python 3: https://www.python.org/downloads/
Download Visual Studio Code: https://code.visualstudio.com/
Get Started with Python in VS Code: https://code.visualstudio.com/docs/py...
———-
Project Structure: 00:51
Topics Covered: 03:08
Getting Started: 04:44
Variables: 06:00
Data Types: 07:10
print(): 09:43
Lists: 11:45
type(): 13:10
Dictionaries: 16:28
input(): 21:42
IF Statements: 22:35
WHILE Loops: 32:38
Booleans: 33:54
Operators: 37:10
pass: 41:00
String Concatenation: 55:20
Imports: 01:08:50
randint(): 01:11:05
Functions: 01:15:05
return: 01:19:50
Arguments & Parameters: 01:21:29
f-strings: 01:29:40
append(): 01:38:40
FOR Loops: 01:44:10
#python
everythingisawsaam
bro im trying to find the command key in my keyboard (i have a windows keyboard) I cant find it. yall know any shortcuts?!?!?!
This tutorial is awesome. You are so educative. There are so many video tutorials that will teach you about an if statement or an list for example but without putting it into a context. With this tutorial you actually get a reward when seeing that the code you do actually do something fun. Thank you!
FINALLY! A video that says it's for beginners that's actually for beginners! Thank you so much! This was very easy to follow.
Hi man, firstly I've had like 4 different types of text based games that I've struggled building in the beginning because of people who don't explain step by step. But you did so much more. You didn't just say copy and paste. You literally taught me as if I was in a class with you alone.
One thing I need help With, the video around 44:20 shows you running the entire thing and when the player's life runs out, it stops. But mine keeps carrying on and shows a "Please select option" but shows absolutely no errors. I've typed out the code just as I understood and like you showed but still
good
In the beginning of the video i was a tad bit annoyed with his accent but by the end of the tutorial, i dont want anyone else to explain computer science to me other than this man and his soothing intellectual voice.
god bless u for this tutorial
I have no exp and it is so good that you explain it so understandable and logical even for empty buckets like me . I am at the while loop and it works !
Tnx Amazing vid u earned a sub
This is an absolute amazing tutorial, after 2 months of trying to learn Python I feel like someone switched on a light bulb in head and that I actually have a chance of learning to code. Thank you so much for your video, it is by far the best one I have seen! Keep up this amazing work!
Ready to boost your career with the most versatile programming language, Python?
Using Python, You can scrape web development, Automate things around you and make them more simple, Adapt Machine learning, etc. by using a python programming language. Python is more capable than we can even explain.
This Python Training program covers the basics of Python and how to adapt it to real-world applications. The modules, classroom projects, and curriculum assignments cover Python data operations, strings, conditional statements, error handling, web scrapings, shell scripting, and Python's Django widely used web frame.
Peopleclick offers python certification training with
• 1 on 1 interaction,
• Quality course materials,
• 24*7 reliable support,
• Multiple live projects.
The trainers here are certified working professionals with years of experience in the industry. Also, we have tied up with 100+ client companies for placement programs. More Than 7000 students are already placed in Top MNCs.
So don’t think twice; grab the opportunity and book your free demo class now. Create A Solid Career With Us.
For More Details: +91 76193 43002
Visit: https://www.peopleclick.in/python-training-bangalore.html?utm_source=SEO&utm_medium=banner&utm_campaign=Python
1:10
Sir, do you have another series of this? You know, like implementing equipment or something like that
Good Teacher rly helped alot
Ok that part game_running = False does not work for me
https://rumble.com/vbi6uz-ayahuasca-manifesto-the-definite-guide-to-using-ayahuasca.html
https://www.bitchute.com/video/9fnwC4Kudxk2/?list=notifications&randomize=false
Can i use Jupyter notebook for this plzzz reply.
How to turn this into a GUI ?
nobody:
academind: "seem like its working but wouldnt be better if we change everything
1:32:00 just marking where I left so that I can continue later
This is one of the best programs I have ever done. It was really simple to follow and the way he explained each step was easy to understand.
I have also added 2 more things:
themselves more than once
once the limit has been reachedit will not increase.the new Diablo IV engine
https://github.com/AyushAryal/chess
Simple chess game using python…hope it helps someone out
10th std anyone??
help, it says a str cannot be subtracted by a str, and when I int them, it says monster['health'] can't be inted
How can make graphic for your game?
Why you didn't run your game?
And how can run the the game after coding?
hallo. endlich mal ein deutscher
Was bashing my head in trying to learn python with books and by trying to be so generalised they dont use any examples that feel real enough. I'm finally feeling like not bashing my head against a brick wall anymore because i see what purpose which line has. Thanks a lot!
dont mind me im just marking where left off 37:06
this is really good, nicely introducing different concepts and constructs. thank you..
15:20 saving my time
Where Is The Command Key?!
anyone else had an issue with syntax error at 58:00 for lines 13 and 14? my error keeps moving on to different keywords like "while", "print" and "str", but disappears when I delete lines 13 and 14. As the error seems to move elsewhere, I have no idea where the actual error is, any ideas?
Where is the game. You have to show us when you were testing it
This clears up alot of misunderstanding I had much ablodged