Coding

Python Tutorial for Beginners – Crash Course 2020 | Build a Game with Python

  • 00:00:01 hi great to have you aboard of this
  • 00:00:03 video do you want to learn – then why
  • 00:00:06 don't you learn it by creating something
  • 00:00:08 entertaining and funny a game for
  • 00:00:10 example this is exactly what we will do
  • 00:00:13 in this video we will build a game in
  • 00:00:16 Python from scratch no prior Python
  • 00:00:19 knowledge is required the game will run
  • 00:00:21 in the terminal and in this game we as
  • 00:00:24 the player will fight against the
  • 00:00:26 monster we will be able to create a
  • 00:00:29 player name we can attack the monster we
  • 00:00:31 can heal ourselves we can't even display
  • 00:00:34 high scores from previous rounds and
  • 00:00:36 more cool things so if you want to learn
  • 00:00:39 – right now this is your chance to do so
  • 00:00:43 [Music]
  • 00:00:45 so before we dive into Python let's have
  • 00:00:49 a look at the actual game that we want
  • 00:00:51 to build the game will actually consist
  • 00:00:53 of a player as basically and a monster
  • 00:00:56 the player has certain attributes you
  • 00:01:00 could say for example the player name
  • 00:01:02 the player could have an attack so an
  • 00:01:04 attack which allows us to well harm the
  • 00:01:07 monster
  • 00:01:08 we could have something like a heal
  • 00:01:09 feature which allows us to increase our
  • 00:01:12 health level and we also need a health
  • 00:01:14 indicator and health status bar which
  • 00:01:17 shows us if you are still alive or if we
  • 00:01:19 are both dead the same thing is true for
  • 00:01:21 the monster the monster also could have
  • 00:01:24 some attributes like name and the health
  • 00:01:27 indicator and also the attack picture
  • 00:01:29 let's say that the monster shouldn't be
  • 00:01:31 allowed to heal itself though so the
  • 00:01:33 monster basically only can attack
  • 00:01:35 ourselves now to control this entire
  • 00:01:38 game flow we need something like a user
  • 00:01:40 interface which allows us to select an
  • 00:01:42 action we are always working from the
  • 00:01:45 player perspective of course this means
  • 00:01:47 an action we can select is basically
  • 00:01:49 only attack or heal so let's say we want
  • 00:01:52 to attack the monster right here if we
  • 00:01:54 do so we will have an impact on to the
  • 00:01:56 health of the monster and decrease the
  • 00:01:59 monsters well health status after this
  • 00:02:02 attack we have to check if the monster
  • 00:02:04 is still alive so if the health level is
  • 00:02:06 smaller than or equal to zero if this is
  • 00:02:10 the case so if we say yes the monster is
  • 00:02:12 basically not
  • 00:02:14 anymore then we have to make sure that
  • 00:02:16 the player wins and that a new round of
  • 00:02:19 the game starts or a new game starts in
  • 00:02:21 general in case the monster is still
  • 00:02:23 alive though
  • 00:02:24 well the monster will attack back
  • 00:02:25 automatically with that the monster bill
  • 00:02:28 of course affect our health negatively
  • 00:02:30 then we have to implement the same check
  • 00:02:32 we had before we have to check if our
  • 00:02:35 health level is below or equal to zero
  • 00:02:38 if that's the case well then
  • 00:02:40 unfortunately the monster won the game
  • 00:02:42 and we have to start a new one if this
  • 00:02:44 is not the case so if you are still
  • 00:02:46 alive well then we go back to our user
  • 00:02:48 interface and we can start the next
  • 00:02:50 round
  • 00:02:50 by selecting another action in our case
  • 00:02:53 this would be attack or heal so that's
  • 00:02:55 the game we want to build in a nutshell
  • 00:02:57 as I said we will build this all from
  • 00:03:00 scratch with Python and for that we need
  • 00:03:03 to use and understand some core Python
  • 00:03:05 features so what will we learn in here
  • 00:03:07 regarding – we will have a look at
  • 00:03:11 variables variables allow us to store
  • 00:03:14 information in our code and to access it
  • 00:03:16 whenever we need it we will also have a
  • 00:03:19 look at user input this means we will
  • 00:03:21 learn how we can make sure that the user
  • 00:03:23 can enter data and that we can react to
  • 00:03:26 the input the user made will also have a
  • 00:03:29 look at operators operators allow us to
  • 00:03:32 subtract or add different items or also
  • 00:03:35 to check whether an item is smaller or
  • 00:03:38 bigger than another item for example
  • 00:03:39 besides that we'll have a look at lists
  • 00:03:42 and dictionaries
  • 00:03:44 dictionaries are special ways you could
  • 00:03:47 say to store information in Python and
  • 00:03:50 also one of the key concepts we will
  • 00:03:52 cover in this video we'll also have a
  • 00:03:54 look at functions in pythons so we will
  • 00:03:56 learn how we can create functions what
  • 00:03:58 functions are and why we need them to
  • 00:04:00 create reusable code definitely one of
  • 00:04:03 the core features of every programming
  • 00:04:06 language basically we'll also have a
  • 00:04:08 look at control structures these are
  • 00:04:10 things like if statements for example
  • 00:04:11 you might have heard about that
  • 00:04:13 this means we execute code if a certain
  • 00:04:16 condition is true well or not and
  • 00:04:18 finally we'll have a look at f strings
  • 00:04:20 so-called formatted string literals
  • 00:04:23 these are things which allow us to
  • 00:04:25 dynamically inject in
  • 00:04:27 from outside into a so-called string
  • 00:04:29 this is a bit more advanced right here
  • 00:04:32 but we will come to a point in the video
  • 00:04:34 where this makes perfect sense to you
  • 00:04:36 now with that we can leave this
  • 00:04:39 theoretical planning part and jump
  • 00:04:40 directly into our code and to get
  • 00:04:44 started you need an IDE in my case I
  • 00:04:46 will use Visual Studio code and you need
  • 00:04:49 – free installed on your machine
  • 00:04:52 installation guidelines for both of
  • 00:04:54 these can be found in the video
  • 00:04:56 description now once you created a new
  • 00:04:59 project in your IDE we can create a
  • 00:05:02 Python file right here we click on to
  • 00:05:05 this button and now we can give this
  • 00:05:07 file any name we want in my case I will
  • 00:05:09 name it game the important thing is that
  • 00:05:11 we add dot py to this file this will
  • 00:05:15 turn it into a file – can read them and
  • 00:05:19 in this file we can now get started and
  • 00:05:21 as I said we need a player and the
  • 00:05:24 monster these are the core components of
  • 00:05:26 our game you could say and the player
  • 00:05:29 and the monster have certain attributes
  • 00:05:31 so if we think about the player we could
  • 00:05:33 say that our player should have a name
  • 00:05:35 and a name could be manual in this case
  • 00:05:38 for example my name now if we save that
  • 00:05:41 you see that we get an error immediately
  • 00:05:43 if we hover over it you can see that we
  • 00:05:45 created an undefined variable manual and
  • 00:05:49 that's the first important concept we
  • 00:05:51 will cover here writing text like this
  • 00:05:53 in our code won't work here we have to
  • 00:05:56 put this information this specific
  • 00:05:58 information into a so-called variable
  • 00:06:01 variable simply allow us to store
  • 00:06:04 information in our code and to access
  • 00:06:06 this information later now to make this
  • 00:06:09 a bit more precise right here we could
  • 00:06:11 say that actually manual should just be
  • 00:06:14 the value or the information that you
  • 00:06:16 want to store right here and this
  • 00:06:18 information should be stored in a
  • 00:06:20 variable which could be named player
  • 00:06:23 underscore name for example that's the
  • 00:06:26 naming convention in Python for
  • 00:06:27 variables we type the name with small
  • 00:06:30 letters and if we have a combination of
  • 00:06:33 different names like player name in here
  • 00:06:35 we connect these different names with an
  • 00:06:37 underscore then we add a space and set
  • 00:06:40 the variable
  • 00:06:41 equal to the information that should be
  • 00:06:44 stored in this variable in that case the
  • 00:06:46 name manual now if we save it now we can
  • 00:06:49 see that the error regarding the
  • 00:06:50 variable right here is gone because we
  • 00:06:52 basically declared that variable so we
  • 00:06:55 assigned a value to this variable manual
  • 00:06:58 in this case but if I hover over manual
  • 00:07:00 right here you can see that this is
  • 00:07:02 still an undefined variable now the
  • 00:07:03 problem is that the information in a
  • 00:07:06 variable has to have a specific data
  • 00:07:09 type data types in Python are numbers so
  • 00:07:12 integers in this case as you can see if
  • 00:07:14 I save this now we don't have an error
  • 00:07:16 these are floats like 1 or 2 for example
  • 00:07:19 this is also working
  • 00:07:21 we have boolean x' a boolean would be
  • 00:07:25 true like that written with a capital
  • 00:07:27 letter at the beginning or false
  • 00:07:30 written like that or as we have in our
  • 00:07:34 case strings strings should be wrapped
  • 00:07:37 in quotation marks this can be double
  • 00:07:39 quotation marks like this or single
  • 00:07:41 rotation marks generally you can use
  • 00:07:44 whichever you want the important thing
  • 00:07:46 is to be consistent though I will stick
  • 00:07:48 to single quotation marks in this
  • 00:07:49 project here now we can add the player
  • 00:07:53 name right here in the petition marks
  • 00:07:55 manual and with that you can see that we
  • 00:07:58 now don't get an error and that manual
  • 00:08:00 now is a string so summarize that we now
  • 00:08:03 declared a variable named player name
  • 00:08:06 and this variable contains information
  • 00:08:09 in this case the string manual that's
  • 00:08:12 basically our first variable now we can
  • 00:08:15 of course create additional variables
  • 00:08:16 here another one could be player attack
  • 00:08:20 for example we talked about that already
  • 00:08:22 and this could have an integer now let's
  • 00:08:25 say 10 we could also add player he'll
  • 00:08:29 for example that we can heal our player
  • 00:08:31 which could be a value of 16 we might
  • 00:08:34 have to work on these values later and
  • 00:08:36 we could add health for example right
  • 00:08:38 here as the last variable which could
  • 00:08:40 have a value of 100 so with that we
  • 00:08:43 basically defined our player right here
  • 00:08:45 this doesn't do a lot on if we now
  • 00:08:48 execute the code that we wrote in this
  • 00:08:51 file we will see that nothing is
  • 00:08:53 displayed down here in our terminal
  • 00:08:55 this is what I talked about at the
  • 00:08:56 beginning of this video that our user
  • 00:08:59 interface will be this part right here
  • 00:09:01 this terminal by the way if you don't
  • 00:09:03 see this terminal you can always make it
  • 00:09:05 appear right here if you go to view and
  • 00:09:07 then click right here on to terminal
  • 00:09:10 this will make it appear down here
  • 00:09:12 however if we now execute this file by
  • 00:09:15 entering – and now the file name so game
  • 00:09:18 dot py like this and it enter you can
  • 00:09:23 see that we don't get an error or
  • 00:09:24 something like that but that nothing is
  • 00:09:26 printed or displayed down here now by
  • 00:09:29 saying printed I already gave you a hint
  • 00:09:31 what we can do next because in Python we
  • 00:09:34 can print or display the output of our
  • 00:09:38 code so to say by adding so-called print
  • 00:09:41 statements now a print statement is
  • 00:09:43 written like that you type print then
  • 00:09:46 you open the quotation marks right here
  • 00:09:48 and then you enter well basically what
  • 00:09:50 you want to print right here this could
  • 00:09:52 be a string for example like Hello if I
  • 00:09:55 now repeat the command right here like
  • 00:09:57 this you can see that we printed hello
  • 00:09:59 down there now in our case we don't want
  • 00:10:02 to print any random string we might want
  • 00:10:05 to print the information that we stored
  • 00:10:08 in a variable for example right here in
  • 00:10:10 our player name for that we remove the
  • 00:10:14 content here and now add player name
  • 00:10:16 right here as you can see we already get
  • 00:10:18 these suggestions in the IDE right here
  • 00:10:20 so this makes it easier to select the
  • 00:10:22 variable we want to refer to him in that
  • 00:10:24 case I will use player name and hit tab
  • 00:10:26 and if we now save that and run the code
  • 00:10:29 again you can see that manual down here
  • 00:10:32 is printed in our console so these are
  • 00:10:36 two important things we learn already we
  • 00:10:39 can use variables and we can print the
  • 00:10:42 output of our code in the terminal by
  • 00:10:45 using such a print statement now the
  • 00:10:47 problem is that actually using for
  • 00:10:50 variables for our player is not really
  • 00:10:53 the most logical approach actually
  • 00:10:56 wouldn't it be better to basically have
  • 00:10:59 one variable player for example and if
  • 00:11:02 we would then have an option to access a
  • 00:11:04 specific information of that variable
  • 00:11:06 for example the attack right
  • 00:11:09 because if I want to do this now I have
  • 00:11:11 to change player name to player attack
  • 00:11:13 right here and then we run the code and
  • 00:11:16 this will return the value for the
  • 00:11:17 attack but as you can see that's not
  • 00:11:20 really convenient and also not a really
  • 00:11:21 nice coding style right here
  • 00:11:23 therefore we need an option to basically
  • 00:11:26 combine all this information in such a
  • 00:11:29 single variable for that let's maybe
  • 00:11:31 create another variable and let's call
  • 00:11:33 it player this time and now set it equal
  • 00:11:35 to well to what and – we have different
  • 00:11:39 options to add multiple information to
  • 00:11:42 such a variable a very common one is a
  • 00:11:45 so called
  • 00:11:46 list a list is always written like that
  • 00:11:49 so you use the square brackets right
  • 00:11:51 here and then you enter content into
  • 00:11:54 your list in our case our list could
  • 00:11:57 have four different types of content it
  • 00:11:59 could have D string Manual which will be
  • 00:12:02 the name and the four at the free
  • 00:12:04 integers 10 16 and 100 so I could now
  • 00:12:08 type manual right here then I could type
  • 00:12:11 10 16 100 that would be possible and if
  • 00:12:17 I now comment out this code right here
  • 00:12:19 this is shift command + 7 right here as
  • 00:12:24 a shortcut that I can use and as you can
  • 00:12:27 see by that we automatically add the
  • 00:12:29 hash tags right here but at the
  • 00:12:32 beginning so this means this code is now
  • 00:12:34 not used you could say so this code will
  • 00:12:36 not be executed basically it's just
  • 00:12:38 there as kind of notes for us or we can
  • 00:12:40 keep the code right here and maybe reuse
  • 00:12:42 it later but it's not used at the moment
  • 00:12:44 and here we get an error now because of
  • 00:12:47 that because as you can see the variable
  • 00:12:48 is no longer defined because we
  • 00:12:50 commented it out but if I now change the
  • 00:12:54 player attack to player right here
  • 00:12:56 save the file always important otherwise
  • 00:12:59 it won't work and now run Python game
  • 00:13:01 again you can see that the content of
  • 00:13:03 our list right here is printed we can
  • 00:13:06 also check the type of the content we
  • 00:13:09 have right here because I'm referring to
  • 00:13:10 this list all the time and if you add
  • 00:13:12 type right here in front of the variable
  • 00:13:15 that you want to print and now add the
  • 00:13:18 the renfa's is right here and if you're
  • 00:13:21 now safe to file and run the code again
  • 00:13:22 you can see that we created a list right
  • 00:13:25 here that's also an important concept
  • 00:13:27 you can always check the type of the
  • 00:13:29 information you stored right here for
  • 00:13:31 example in your variable by using this
  • 00:13:33 type method right here however we don't
  • 00:13:37 need that here we only need to print it
  • 00:13:39 and the problem is also that we don't
  • 00:13:41 want you access the entire content as
  • 00:13:44 before we might want to access the name
  • 00:13:46 only right here we can do this with
  • 00:13:49 lists for that we simply add the square
  • 00:13:53 brackets again right here and now we can
  • 00:13:56 basically select a specific item in our
  • 00:14:00 list by using a so called
  • 00:14:02 index the index always starts with zero
  • 00:14:05 in such a list what does this mean this
  • 00:14:08 basically so the first item has an index
  • 00:14:11 of zero the second item ten right here
  • 00:14:15 has an index of 1 and the third item has
  • 00:14:19 an index of two before if item has an
  • 00:14:21 index of 3 and so on
  • 00:14:22 so this means if I now simply add 0
  • 00:14:25 right here so the index of 0 right there
  • 00:14:28 and now run our code once again you can
  • 00:14:31 see that manual is printed right here in
  • 00:14:34 our console if you change it to 2 for
  • 00:14:37 example then what is printed well I hope
  • 00:14:39 you guessed it it's 16 now because we
  • 00:14:42 have the index to sew deferred item in
  • 00:14:44 our list right here so with that we now
  • 00:14:47 achieved a lot already we are now able
  • 00:14:49 to store all the information that is
  • 00:14:51 related to our player in such a list and
  • 00:14:54 we can access it by simply using this
  • 00:14:56 player variable and then using the index
  • 00:14:58 to print a specific item in that list
  • 00:15:01 now besides accessing it we can also
  • 00:15:04 change the value that we have in such a
  • 00:15:06 list we can do this by simply typing
  • 00:15:08 player then again the square brackets
  • 00:15:11 and now again the index that you might
  • 00:15:13 want to change let's say we are not
  • 00:15:15 happy with our value for the attack
  • 00:15:18 right here let's say we want to set it
  • 00:15:19 equal to 12 for example for that we
  • 00:15:22 access our current list item with the
  • 00:15:25 index 1 and now set it equal to 11 maybe
  • 00:15:29 like that if I now print there
  • 00:15:32 right here so we print the item with two
  • 00:15:36 index one again
  • 00:15:37 and if we not execute that code you can
  • 00:15:39 see that we change the content of this
  • 00:15:41 list item to 11 however let's say we are
  • 00:15:45 happy with that and we don't want to
  • 00:15:46 change the value because there is
  • 00:15:49 another problem right here although we
  • 00:15:52 stored the information in our list we
  • 00:15:54 don't actually see what the actual well
  • 00:15:57 value is I mean it's obvious for us that
  • 00:15:59 this is a name but how should we know
  • 00:16:01 that 10 right here refers to an attack
  • 00:16:03 right here this refers to the amount we
  • 00:16:06 can heal ourselves and that this is our
  • 00:16:09 health bar that's not totally clear
  • 00:16:11 right here therefore a list is not the
  • 00:16:14 perfect solution in this case so let's
  • 00:16:16 also comment out this year because
  • 00:16:19 besides lists we also have another
  • 00:16:21 really common option to store data right
  • 00:16:24 here in Python these are so-called
  • 00:16:27 dictionaries a dictionary works like
  • 00:16:30 this you type player and now set it
  • 00:16:33 equal to content in curly braces between
  • 00:16:36 these curly braces you now don't add the
  • 00:16:38 items as before so with like manual
  • 00:16:41 right here then a comma and then the
  • 00:16:43 next item it doesn't work like that
  • 00:16:45 because dictionaries always work with
  • 00:16:48 so-called key value pairs that what's a
  • 00:16:51 key what's a value well in our case we
  • 00:16:54 store the name as a value so our key
  • 00:16:56 would be the name it's type name right
  • 00:16:59 here it's important in the quotation
  • 00:17:01 marks it is important but then we don't
  • 00:17:04 add a comma or something like that
  • 00:17:06 we add a colon right here and then we
  • 00:17:08 add the value that belongs to that key
  • 00:17:11 in our case this is manual so the name
  • 00:17:14 like that then we add a comma and then
  • 00:17:18 we add the second key in our case this
  • 00:17:20 would be attack like that then we had a
  • 00:17:24 colon and then we add the value for our
  • 00:17:26 attack this is 10 in our case no
  • 00:17:29 quotation marks are required of course
  • 00:17:31 because we have an integer here after
  • 00:17:34 that we have a heal key this one right
  • 00:17:38 here so quotation marks colon and the
  • 00:17:42 value and finally we have our health
  • 00:17:45 at there : and 100 so this is now a
  • 00:17:50 so-called dictionary as you saw
  • 00:17:52 dictionaries work different than lists
  • 00:17:54 and this also refers to the way we can
  • 00:17:57 access our items in the dictionary if we
  • 00:18:00 for example run our code once again then
  • 00:18:04 you see that we get a key error right
  • 00:18:06 here so we basically can't access our
  • 00:18:09 dictionary with this index right here
  • 00:18:12 this doesn't work the reason for that is
  • 00:18:14 that we need to access the values that
  • 00:18:17 we stored in our dictionary by the key
  • 00:18:19 so in our case we can simply say that if
  • 00:18:22 we want to access the player name right
  • 00:18:24 here and they finna run the code like
  • 00:18:27 this you can see that we print the name
  • 00:18:29 manual right here once again we can also
  • 00:18:33 access different values of course by
  • 00:18:35 simply changing the key this could be
  • 00:18:36 something like attack ready for example
  • 00:18:38 if we now run the code we can access the
  • 00:18:41 value of 10 now such a dictionary is of
  • 00:18:44 course a very better approach to access
  • 00:18:46 the information we have in the
  • 00:18:47 dictionary because throughout the game
  • 00:18:49 we will have to access our attack we
  • 00:18:51 will then have to access the impact that
  • 00:18:53 the attack has on the health of the
  • 00:18:54 monster and so on so with this
  • 00:18:57 dictionary we actually got a nice way of
  • 00:18:59 storing our player and monster related
  • 00:19:02 information now because of that we can
  • 00:19:05 delete all that out commented code right
  • 00:19:07 here like that and now we can also add
  • 00:19:11 our monster right here it follows the
  • 00:19:14 same logic actually so basically our
  • 00:19:16 monster is equal to a dictionary and the
  • 00:19:19 monster will also have a name key like
  • 00:19:23 that and let's give it a name of maybe
  • 00:19:25 max for example
  • 00:19:27 then we have an attack also for the
  • 00:19:30 monster which could have a value of
  • 00:19:32 maybe let's say it's a bit stronger 12
  • 00:19:35 we might have to adjust these values
  • 00:19:37 later so let's use these as dummy values
  • 00:19:39 for the moment then we have well we
  • 00:19:42 don't have a heal option because it's a
  • 00:19:44 the monster can only attack the player
  • 00:19:46 and can't heal itself and this means we
  • 00:19:49 only need the health right here at the
  • 00:19:51 end with a value starting value of 100
  • 00:19:54 also now we got the information we need
  • 00:19:57 for the monster and the player but so
  • 00:19:59 far the user doesn't see anything right
  • 00:20:01 here on the screen you can see the
  • 00:20:03 different values depending on the key we
  • 00:20:05 access but actually that's not
  • 00:20:07 information that we want to display when
  • 00:20:09 the game started what we actually would
  • 00:20:11 like to have is some kind of user
  • 00:20:13 interface which guides the user to
  • 00:20:15 select a specific option in the case of
  • 00:20:18 the player this can only be attack or
  • 00:20:20 heal these are the two options we have
  • 00:20:22 but we can use the print statements we
  • 00:20:24 learn already – well create such a user
  • 00:20:27 interface let's say we first want to
  • 00:20:29 print something like please select
  • 00:20:31 action we learned that we can refer to
  • 00:20:34 variables and to print the information
  • 00:20:36 stored in the variables already but of
  • 00:20:39 course if we add a single quotation mark
  • 00:20:40 as I showed you some minutes ago and add
  • 00:20:42 a string right here then we can also
  • 00:20:44 print this information so as I said we
  • 00:20:46 can say please select action like that
  • 00:20:50 and then we can add two more print
  • 00:20:53 statements with the first one saying
  • 00:20:58 something like attack right and the
  • 00:21:02 second option would be to heal player or
  • 00:21:08 just heal maybe like that if we save
  • 00:21:11 that and run our code once again
  • 00:21:12 then you can see that these statements
  • 00:21:14 are printed right here in our terminal
  • 00:21:17 and this should now be the starting
  • 00:21:19 screen the player sees when he starts
  • 00:21:21 the game the problem now is that well we
  • 00:21:23 basically can't interact with that as
  • 00:21:25 you can see we don't have any options to
  • 00:21:27 we'll add information right here or
  • 00:21:28 something like that so we basically only
  • 00:21:30 have these print statements therefore we
  • 00:21:33 need to allow our player to add some
  • 00:21:35 input to our game right here and this
  • 00:21:38 brings us to another default method pipe
  • 00:21:41 with the input method the input method
  • 00:21:44 is simply well written like that input
  • 00:21:46 and now by adding the parentheses right
  • 00:21:48 here and that's it already with that if
  • 00:21:51 we save that now run the code again you
  • 00:21:55 can see down here that we now have the
  • 00:21:57 option to enter some well information
  • 00:21:59 material to add some input this could be
  • 00:22:01 1 2 3 whatever in our case we should
  • 00:22:04 only be able to select 1 or 2 also
  • 00:22:06 something we'll work on later but if I
  • 00:22:08 now add 1 right here at the input and
  • 00:22:10 then we exit the game already
  • 00:22:12 now this exiting problem is something we
  • 00:22:14 will also have a look at in a few
  • 00:22:15 minutes for the moment let's make sure
  • 00:22:17 that something happens after the player
  • 00:22:20 selected a specific input for that we
  • 00:22:24 need a so called control structure in
  • 00:22:26 Python control structures are for loops
  • 00:22:30 while loops or that's one we need right
  • 00:22:33 here if statements an if statement
  • 00:22:35 simply allows you to execute a specific
  • 00:22:38 code that you define of course if a
  • 00:22:41 certain condition or multiple conditions
  • 00:22:43 are true now let's create such an if
  • 00:22:45 statement to see how this works in our
  • 00:22:46 case to create an if statement you
  • 00:22:49 simply type if and now you add the
  • 00:22:52 condition that should be true to make
  • 00:22:54 sure that the code you then define is
  • 00:22:56 running in our case the input right here
  • 00:22:59 so basically the information we returned
  • 00:23:02 right here should be equal to 1 right so
  • 00:23:04 if the player entered run in this should
  • 00:23:07 then lead to an attack against them
  • 00:23:09 against the monster we are gonna need to
  • 00:23:11 declare a variable right here and this
  • 00:23:14 variable now could be player choice for
  • 00:23:17 example
  • 00:23:19 like that with that we can now refer to
  • 00:23:22 our player choice right here and if this
  • 00:23:26 player choice is equal now important not
  • 00:23:29 with a single equal sign because this
  • 00:23:31 will basically set the variable equal to
  • 00:23:34 well something in our case we don't want
  • 00:23:37 to change the information stored in the
  • 00:23:39 variable we only want to check if the
  • 00:23:42 information stored in the variable is
  • 00:23:43 equal to something this means we have to
  • 00:23:46 add a second equal sign and now say if
  • 00:23:48 our player choice is equal to one then
  • 00:23:52 we have to add a colon and then if you
  • 00:23:54 would enter you can see that we get this
  • 00:23:56 automatic indentation right here this is
  • 00:23:58 important in Python we basically use
  • 00:24:02 indentation to specify where our code
  • 00:24:06 belongs to in our case this means that
  • 00:24:08 this code right here which for example
  • 00:24:10 could be a print statement saying attack
  • 00:24:13 right here only gets executed if this
  • 00:24:18 condition is true so if this is true
  • 00:24:20 execute the code this indented code
  • 00:24:22 right here if we run our code now you
  • 00:24:25 can see if we get one attack well we
  • 00:24:29 don't get the print statement right here
  • 00:24:31 right so this doesn't work here we only
  • 00:24:34 get one this was our input but nothing
  • 00:24:36 is displayed and actually our player
  • 00:24:38 choice was equal to one but this is not
  • 00:24:40 working the problem here is that input
  • 00:24:44 right here always returns a string and
  • 00:24:47 this means we compare player choice
  • 00:24:50 right here to an integer now player
  • 00:24:53 choice is not equal to this integer to
  • 00:24:56 you to one therefore this code right
  • 00:24:58 here is not executed again you can also
  • 00:25:01 check this by adding type um player
  • 00:25:05 choice like that and if you now say that
  • 00:25:08 you print this statement first right
  • 00:25:11 here then you can see if you had one
  • 00:25:13 that will return a string right here
  • 00:25:15 that's what I was referring to
  • 00:25:17 therefore this check doesn't work at the
  • 00:25:19 moment now let's get rid of that code
  • 00:25:22 and let's now simply turn this one right
  • 00:25:25 here into a string by adding single
  • 00:25:27 quotation marks like that if we do this
  • 00:25:30 and vulnera once again
  • 00:25:32 add it attack then you can see that
  • 00:25:35 attack is printed right here and with
  • 00:25:38 that we now wrote our first successful
  • 00:25:40 if statement right here now with the new
  • 00:25:43 knowledge we can of course also add the
  • 00:25:45 second condition we have right here if
  • 00:25:46 the player selects you we want to heal
  • 00:25:49 our player for that we simply enter
  • 00:25:52 additional code but now important not
  • 00:25:55 indented because as I said the indented
  • 00:25:58 code will run if this condition right
  • 00:26:00 here this one is true therefore we have
  • 00:26:03 to start right here and now add a
  • 00:26:06 so-called Elif statement Elif simply
  • 00:26:10 stands for else if so basically if this
  • 00:26:13 condition is true do this else so Elif
  • 00:26:18 another condition is true then well
  • 00:26:21 execute another code now this other
  • 00:26:23 condition right here is pretty
  • 00:26:24 straightforward for us of course we
  • 00:26:26 simply have to check if our player
  • 00:26:28 choice is equal important single
  • 00:26:30 quotation mark to and if this is true
  • 00:26:33 then we want to print let's say he'll
  • 00:26:35 player like that and we could also say
  • 00:26:39 that if our user enters any different
  • 00:26:42 input so not one or two then something
  • 00:26:45 like invalid input should be printed to
  • 00:26:47 our console to our terminal down here so
  • 00:26:50 with that again not indented we add else
  • 00:26:53 right here this time else is different
  • 00:26:56 from Elif as else doesn't require any
  • 00:26:59 additional condition it simply says well
  • 00:27:01 if this is not true and well if that is
  • 00:27:04 not true well then in all other cases
  • 00:27:06 simply execute a different code in our
  • 00:27:09 case this could be as I said print
  • 00:27:11 invalid oops
  • 00:27:13 invalid input right here like that let's
  • 00:27:18 now run this code so again so if we
  • 00:27:20 enter one we can see that we print
  • 00:27:23 attack if you run the code again and two
  • 00:27:26 we run he'll player and if we enter
  • 00:27:29 anything else like five for example we
  • 00:27:31 get the output invalid input here now
  • 00:27:35 with that we got our first logic in here
  • 00:27:37 let's now focus on to our first
  • 00:27:40 condition right here so if the player
  • 00:27:41 shows is equal to one because these
  • 00:27:43 print statements are nice but actually
  • 00:27:46 that's not
  • 00:27:47 what should happen here because if the
  • 00:27:49 player choice was one then we want to
  • 00:27:51 attack the monster this means we want to
  • 00:27:54 access the attack key right here in our
  • 00:27:56 player with the value 10 and after that
  • 00:27:59 we want to access our health key right
  • 00:28:02 here in our monster variable and
  • 00:28:05 decrease the monster health by this 10
  • 00:28:08 right here or whatever value we use for
  • 00:28:11 our attack right here now how can we do
  • 00:28:13 this well let's maybe space here and
  • 00:28:17 let's now delete that print statement
  • 00:28:20 because we learned everything we need to
  • 00:28:22 achieve this the first thing we need to
  • 00:28:24 do is we need to access our monster
  • 00:28:27 health because that should change in the
  • 00:28:28 end for that we can again refer to this
  • 00:28:31 monster variable right here and now add
  • 00:28:34 the square brackets right here and
  • 00:28:37 access our health key we learned that
  • 00:28:40 already so we simply type health right
  • 00:28:42 here this health should now be equal to
  • 00:28:45 something else this means we don't want
  • 00:28:48 to compare it to anything we want to set
  • 00:28:50 it equal to something so we want to
  • 00:28:51 change the information stored in that
  • 00:28:54 health key for that we use a single
  • 00:28:56 equal sign and now say that again the
  • 00:28:59 monster health right here so important
  • 00:29:03 I'm not referring to the initial status
  • 00:29:06 of the monster health so this 100 right
  • 00:29:09 here so this initial value for the
  • 00:29:12 monster health should decrease by well
  • 00:29:15 by what well minus the player right our
  • 00:29:19 player variable again the square
  • 00:29:21 brackets and now attack so basically by
  • 00:29:25 the value that is stored in our attack
  • 00:29:28 key now after that let's say we want to
  • 00:29:31 print our monster health once again to
  • 00:29:35 see if this operation was successful for
  • 00:29:39 that we run the game once again we now
  • 00:29:41 attack the monster and as you can see
  • 00:29:44 apparently it worked because now the
  • 00:29:46 monster health decreased to 90 so this
  • 00:29:50 is pretty cool but actually the monster
  • 00:29:52 is not the only party which can be
  • 00:29:54 attacked right here the player can also
  • 00:29:56 be attacked now we can use the same
  • 00:29:59 logic actually we have right here
  • 00:30:00 let's maybe add the space down here and
  • 00:30:03 let's now say that after the monster was
  • 00:30:05 attacked by the player well the monster
  • 00:30:06 will fight back so also the player
  • 00:30:08 health will decrease now for that we can
  • 00:30:11 use the exact same logic we can simply
  • 00:30:14 say that our player health right here is
  • 00:30:17 equal to our player health so the
  • 00:30:20 initial value we have for the player
  • 00:30:22 health
  • 00:30:23 – our well monster attack right here
  • 00:30:28 like that and let's know maybe also add
  • 00:30:32 the player health to our terminal down
  • 00:30:36 there to see if it was successful and
  • 00:30:40 let's then check what happens so if we
  • 00:30:42 run the game once again the player
  • 00:30:45 attacks the monster and the monster
  • 00:30:47 fights back this means after this round
  • 00:30:51 the monster health is at 90 which makes
  • 00:30:54 sense we had an attack of 10 against
  • 00:30:56 this health of 100 and the player health
  • 00:30:59 is at 88 because the monster attack of
  • 00:31:01 12 well had an impact to our initial
  • 00:31:03 health level of 100 so if you run the
  • 00:31:06 game once again and attack again well we
  • 00:31:10 can see that we got the same value and
  • 00:31:12 the reason for that is that we always
  • 00:31:14 exit our game after the first attack was
  • 00:31:18 done or the first two attacks one for
  • 00:31:20 the player one but a monster we're done
  • 00:31:21 because what – at the moment does is it
  • 00:31:24 basically scans our code from top to
  • 00:31:26 bottom
  • 00:31:27 Sisu k we have a player variable we have
  • 00:31:29 a month's variable we print this code
  • 00:31:31 right here then we have an input and
  • 00:31:34 then we have this if statement right
  • 00:31:37 here so if we select one Python symbol
  • 00:31:40 you see is okay we selected one so I
  • 00:31:42 will run this code I will print monster
  • 00:31:44 and player and then I will stop
  • 00:31:47 executing the code because we are done
  • 00:31:49 there is nothing to tell – please repeat
  • 00:31:52 the code or anything like that now this
  • 00:31:54 brings us to the next control structure
  • 00:31:57 we need right here we need a way to tell
  • 00:32:00 python that it shouldn't exit the game
  • 00:32:03 after first attack was done but it
  • 00:32:05 should continue the game until a certain
  • 00:32:07 exit condition that we define and exit
  • 00:32:10 condition if you remember back the slide
  • 00:32:11 would be something like the health of
  • 00:32:14 either the monster or the player is
  • 00:32:16 equal to or smaller than zero this is
  • 00:32:19 something we'll implement in a few
  • 00:32:20 minutes let's focus on to this logic to
  • 00:32:22 stay in the game or to keep executing
  • 00:32:25 the code until we tell – that we are
  • 00:32:27 done and that we want to exit the game
  • 00:32:29 now we learned about the if statement
  • 00:32:31 already which allows us to create this
  • 00:32:33 structure right here another control
  • 00:32:35 structure is a so called Y loop a code
  • 00:32:38 running inside of such a while loop is
  • 00:32:41 executed as long as a condition that we
  • 00:32:44 define is true now this sounds kind of
  • 00:32:47 abstract so let's create this Y loop now
  • 00:32:50 and then see why this is exactly what we
  • 00:32:53 might need right here for that we will
  • 00:32:55 add the new code up here because when we
  • 00:32:58 initialize our game this information is
  • 00:33:00 important for – it has to know the
  • 00:33:03 initial values for all variables but
  • 00:33:05 once this initialization was done we
  • 00:33:07 only want to execute code that is
  • 00:33:10 running inside of our while loop as long
  • 00:33:12 as this while loop is true our game is
  • 00:33:13 running if we exit the while loop well
  • 00:33:15 the game is no longer running so we
  • 00:33:18 create a while loop by simply typing
  • 00:33:20 while right here and now we have to
  • 00:33:22 define a condition and in our case the
  • 00:33:26 condition could actually be something
  • 00:33:27 like well while our game is running well
  • 00:33:31 execute that code as soon as our game is
  • 00:33:34 no longer running well then don't
  • 00:33:36 execute this code again this means we
  • 00:33:39 can actually declare another variable up
  • 00:33:42 here before we create a while loop and
  • 00:33:45 this could be something like game
  • 00:33:47 running equals well true now what is
  • 00:33:51 true now true is a so-called boolean
  • 00:33:54 boolean x' can be either true or false
  • 00:33:57 I showed you that in the beginning
  • 00:33:58 already these allow us to easily control
  • 00:34:01 while loops for example because if we
  • 00:34:04 say that the initial value for game
  • 00:34:06 running is true well we can simply add
  • 00:34:09 this logic right here to say game
  • 00:34:10 running equals true so basically as long
  • 00:34:14 as you don't change the value right here
  • 00:34:15 so it's true here well please keep it
  • 00:34:17 running but if we want to exit this loop
  • 00:34:20 now we only have to change game running
  • 00:34:22 from true to false and with that this
  • 00:34:25 condition is no longer met and we exit
  • 00:34:27 our while loop
  • 00:34:28 immediately important though to be able
  • 00:34:31 to tell Pyfrom that our code should only
  • 00:34:33 be executed inside our while loop or
  • 00:34:36 that the code belongs to this while loop
  • 00:34:37 basically we have to indent the code
  • 00:34:40 this means we can select everything
  • 00:34:42 right here so all the code we wrote so
  • 00:34:44 far and now it tap to basically indent
  • 00:34:46 it now if you now save this we shouldn't
  • 00:34:49 get an error and as you can see if we
  • 00:34:52 now run the code – game attack attack we
  • 00:34:56 stay inside our loop and important the
  • 00:34:59 health values also decrease as you can
  • 00:35:02 see the monster has help of 18 and the
  • 00:35:03 player of 76 which makes sense after two
  • 00:35:06 rounds because the monster received two
  • 00:35:09 attacks 10 each from player and the
  • 00:35:12 player received two attacks from the
  • 00:35:14 monster with 12 each so 99 – sorry 80
  • 00:35:20 versus 76 and if you continue attacking
  • 00:35:22 this decreases more and more we can also
  • 00:35:25 get to health levels below zero as I
  • 00:35:28 said this is something we'll address
  • 00:35:30 soon the problem is that we don't exit
  • 00:35:33 our loop till now for that as I said we
  • 00:35:35 only need to refine a case where this
  • 00:35:37 true changes to false for that we could
  • 00:35:40 add another if statement so after these
  • 00:35:43 statements right here if our player
  • 00:35:49 health right here is smaller than or
  • 00:35:55 equal to zero well then we simply say
  • 00:35:58 that game running should be set to false
  • 00:36:02 like that if we now exit our game we can
  • 00:36:08 do this by hitting ctrl + Z right here
  • 00:36:10 no here this may be and now run the code
  • 00:36:12 once again we can attack attack again
  • 00:36:15 again again again again again
  • 00:36:18 oops sorry invalid input so again again
  • 00:36:22 and let's see yep now we can see that we
  • 00:36:26 reached a health level of -8 for our
  • 00:36:29 player and because of that we exited our
  • 00:36:32 game why did this happen just repeat
  • 00:36:34 that we said that the game running
  • 00:36:37 variable is set to false as soon as our
  • 00:36:40 player is that basically
  • 00:36:42 this is the sign for our while loop to
  • 00:36:44 stop being executed and with that we're
  • 00:36:47 basically outside of our loop well and
  • 00:36:50 with that we basically are outside of
  • 00:36:52 our code because there is no more code
  • 00:36:54 to run at the moment for Python now by
  • 00:36:58 the way we used this double equal sign
  • 00:37:01 right here right these two we also used
  • 00:37:03 this right here this smaller than or
  • 00:37:05 equal sign these are so-called
  • 00:37:07 comparison operators in Python we saw
  • 00:37:10 two of these already we also have more
  • 00:37:13 we also have something like smaller than
  • 00:37:15 larger than larger than or equal to and
  • 00:37:18 we also got this this one right here
  • 00:37:21 which basically means not equal to so
  • 00:37:25 these are operators we can use to
  • 00:37:27 basically check if a specific value is
  • 00:37:31 equal to another value so as you can see
  • 00:37:34 right here these are really helpful
  • 00:37:35 besides these comparison operators let
  • 00:37:39 me change the back to smaller than and
  • 00:37:40 equal to we also have another really
  • 00:37:43 helpful operator type in – these so
  • 00:37:47 called logical operators and these can
  • 00:37:49 be helpful right now because at the
  • 00:37:51 moment we only check if our player
  • 00:37:53 health is smaller than or equal to zero
  • 00:37:55 but actually if our months is that we
  • 00:37:57 also want to stop executing our code and
  • 00:38:00 geological operators are simply and so
  • 00:38:04 this means it would check if the player
  • 00:38:06 is that and if our monster health right
  • 00:38:12 here it's also smaller than or equal to
  • 00:38:16 zero but that's basically not what you
  • 00:38:18 want right here because you basically
  • 00:38:19 want to exit the game if either of the
  • 00:38:22 two is dead and not a faux fur that we
  • 00:38:24 could also use not right here which
  • 00:38:27 would simply check if the player health
  • 00:38:30 if the player is dead and if the monster
  • 00:38:32 is not that so if the monster is still
  • 00:38:34 alive also not 51 right here
  • 00:38:36 and we can also use or another logical
  • 00:38:40 operator which simply says check if
  • 00:38:43 either this condition right here this
  • 00:38:45 one or that condition is true and if
  • 00:38:48 that's the case please execute the code
  • 00:38:50 right here now if we quickly change our
  • 00:38:53 player attack right here to allow
  • 00:38:55 above the one from the monster 213 for
  • 00:38:58 example and run the code once again you
  • 00:39:01 can see that D monster health the first
  • 00:39:04 one right here it's at 87 and so on this
  • 00:39:06 and like this and like that and now as
  • 00:39:09 you can see our monster is dead appear
  • 00:39:12 and we stop our code execution so the
  • 00:39:15 logical operator right here also pretty
  • 00:39:17 helpful if you have situations like that
  • 00:39:19 or you want to run a code depending on
  • 00:39:22 whether one of multiple conditions is
  • 00:39:25 met however our current code has a
  • 00:39:29 logical issue because we do check if the
  • 00:39:32 player or the monster is that down here
  • 00:39:34 but wouldn't it make more sense to
  • 00:39:37 actually check this after each attack
  • 00:39:39 because let's assume that our monster
  • 00:39:42 health decreased below zero after the
  • 00:39:44 first attack our player made this can
  • 00:39:46 happen right here right so it is
  • 00:39:47 possible that this code runs then the
  • 00:39:49 monster has a health level below zero
  • 00:39:51 but what will happen then is that the
  • 00:39:53 player can still be attacked by the
  • 00:39:55 monster we don't have any logic to check
  • 00:39:57 if the monster is able to make his
  • 00:40:01 attack because well it's only checked
  • 00:40:03 after this if condition was one so after
  • 00:40:06 we entered the command we make our two
  • 00:40:08 attacks one by the player one more
  • 00:40:10 monster and then we check if the game
  • 00:40:12 can continue running and this doesn't
  • 00:40:15 make a lot of sense actually so adding
  • 00:40:17 the check actually well right here after
  • 00:40:20 the monster attack and right there after
  • 00:40:23 the player attack is what we should do
  • 00:40:24 next
  • 00:40:25 for that we can use our new knowledge
  • 00:40:27 that we gained regarding boolean's right
  • 00:40:29 here because why don't we use something
  • 00:40:32 like a player one boolean and a monster
  • 00:40:35 one boolean and then we add an if
  • 00:40:37 condition right here to check if the
  • 00:40:39 monster is still alive so basically we
  • 00:40:41 do something like if monster health
  • 00:40:48 right here is smaller than or equal to
  • 00:40:51 zero then we have to do something we
  • 00:40:53 basically have to end the game we'll
  • 00:40:55 implement that code soon but for a
  • 00:40:57 moment let's simply create this
  • 00:40:58 placeholder and to do this we can use
  • 00:41:00 pass right here
  • 00:41:02 so what pass simply does is it basically
  • 00:41:05 shows Python okay
  • 00:41:07 there is some code required right here
  • 00:41:09 at the stage I did not create that code
  • 00:41:11 at the moment so Python simply sees okay
  • 00:41:13 we need some code right here but it
  • 00:41:15 doesn't throw an error it basically
  • 00:41:17 understands that we have a placeholder
  • 00:41:18 right here you could say and therefore
  • 00:41:20 it keeps executing the following code
  • 00:41:22 without an error because if you would
  • 00:41:24 not add this right here you would get an
  • 00:41:26 error and if that is not true so if the
  • 00:41:32 monster is alive well then the monster
  • 00:41:34 can attack back so we can add else right
  • 00:41:36 here
  • 00:41:36 no Elif statement required because we
  • 00:41:39 basically need to say only okay the
  • 00:41:41 monster is alive so let's attack the
  • 00:41:42 player so else we say the player health
  • 00:41:45 should decrease and afterwards we of
  • 00:41:49 course also have to check if the player
  • 00:41:53 health right here is smaller than or
  • 00:41:58 equal to zero like that and we can also
  • 00:42:01 add pass for the moment because we will
  • 00:42:03 add the code in a few seconds these
  • 00:42:06 print statements can stay here for the
  • 00:42:08 moment we'll also put them to a
  • 00:42:10 different position later now these are
  • 00:42:13 the if checks I talked about but I also
  • 00:42:15 mentioned something like player 1 or
  • 00:42:17 monster 1 these follow the same logic we
  • 00:42:22 have appear for game running the only
  • 00:42:24 difference is of course that we will
  • 00:42:26 basically declare that variable inside
  • 00:42:29 our game because we don't need to add
  • 00:42:31 these right here because the information
  • 00:42:33 if the player 1 or not is not required
  • 00:42:35 outside our actual game logic right here
  • 00:42:38 but it's part of the actual game so
  • 00:42:40 let's say as long as the game is running
  • 00:42:42 the initial value for player 1 should be
  • 00:42:46 equal to false because when the game
  • 00:42:48 starts or as long as the game is running
  • 00:42:49 nobody won
  • 00:42:51 and the same thing is true for our
  • 00:42:52 monster 1 variable right here this
  • 00:42:55 should also be set to false like that
  • 00:42:57 now with these two variables defined we
  • 00:43:01 can
  • 00:43:01 our quadrant here and say that if our
  • 00:43:04 monster is dead
  • 00:43:05 well then player one right here should
  • 00:43:09 be set to true because well the monster
  • 00:43:13 died player one and the same logic also
  • 00:43:16 applies right here if the player is dead
  • 00:43:18 if the player is that then our monster
  • 00:43:21 one should be set to true also right
  • 00:43:24 because then we can use this logic right
  • 00:43:28 here and go down to this if check and
  • 00:43:32 basically change this logic to simply
  • 00:43:35 player
  • 00:43:38 one equals true or monster one equals
  • 00:43:46 true right like that well then the game
  • 00:43:49 ends so let's see if this works still
  • 00:43:53 let's run the code that's tak it's
  • 00:43:56 attack let's attack or attack like that
  • 00:44:01 and now if we hit one once again we can
  • 00:44:05 see that the game was accident because
  • 00:44:07 our player won the game as you can see
  • 00:44:09 right here the monster only has a health
  • 00:44:11 of minus 4 so it's basically that so
  • 00:44:14 with that we refactored our code we now
  • 00:44:17 have these checks at the right position
  • 00:44:18 another thing that I don't like though
  • 00:44:20 is that we basically exit the game as
  • 00:44:23 soon as the player or the monster won
  • 00:44:25 basically it's correct that's what we
  • 00:44:27 wanted to implement so I wanted to kind
  • 00:44:30 of escape this while loop right here
  • 00:44:32 because we say that the game stops as
  • 00:44:34 soon as somebody won but actually it
  • 00:44:36 shouldn't be the entire game that stops
  • 00:44:38 after somebody won it should actually be
  • 00:44:41 only the current round that is stopped
  • 00:44:43 because what we actually need is we need
  • 00:44:45 to make sure that as soon as the player
  • 00:44:47 or the monster won the default values
  • 00:44:50 for the health so 100 for the monster
  • 00:44:52 and the player are set again and with
  • 00:44:55 that we should be able to start the next
  • 00:44:56 round immediately and not exiting the
  • 00:44:58 entire game and then having to enter
  • 00:45:00 pipe from game py again to start the
  • 00:45:02 game that's not what we want but we can
  • 00:45:05 implement that easily now with our
  • 00:45:07 knowledge that we gained so far because
  • 00:45:09 why don't we just add a second while
  • 00:45:12 loop which is something like new round
  • 00:45:15 so as long as new round is true we keep
  • 00:45:18 running our current game as soon as new
  • 00:45:22 round is false so as soon as the monster
  • 00:45:25 or the player won we exit the current
  • 00:45:27 round and start a new game now let's
  • 00:45:30 have a look at how we could implement
  • 00:45:32 that because we could say right here
  • 00:45:36 after our first while loop we add this
  • 00:45:38 while loop so let's say while new round
  • 00:45:41 is equal to true right this should
  • 00:45:45 happen so let's maybe indent our code
  • 00:45:48 right here once again like that and we
  • 00:45:51 can define the new round right here
  • 00:45:53 inside our first while loop so new round
  • 00:45:56 should be equal to true at the beginning
  • 00:45:59 now with that we can say that as long as
  • 00:46:02 scheme running is set to true which we
  • 00:46:04 defined right here new round is also set
  • 00:46:07 true and as long as new round is set to
  • 00:46:09 true this code should be executed this
  • 00:46:13 means if we simply change down here our
  • 00:46:15 game running right to new round right
  • 00:46:20 here we should be able to exit our round
  • 00:46:24 as soon as the monster or the player won
  • 00:46:26 and then we will go back to our game and
  • 00:46:29 this means the logic that we exit the
  • 00:46:31 entire game as soon as somebody one
  • 00:46:33 should now be fixed now let's see if you
  • 00:46:35 run Python game and click attack
  • 00:46:38 multiple times
  • 00:46:41 hmmm this is not working the code keeps
  • 00:46:46 getting executed and we don't have any
  • 00:46:48 kind of hint that we actually exit this
  • 00:46:50 second loop right here the problem here
  • 00:46:53 is this part right here our variables
  • 00:46:56 because we never leave this first while
  • 00:46:59 loop we basically set game running to
  • 00:47:02 true then as long as this is true we say
  • 00:47:05 that neuron to be true then we say as
  • 00:47:07 long as this is the case please execute
  • 00:47:09 this code then we set new round to false
  • 00:47:12 and what happens then is we go back to
  • 00:47:14 this first right loop this is nice but
  • 00:47:17 what is missing here is the resetting of
  • 00:47:19 the values so we basically continue
  • 00:47:22 using the values the changed values
  • 00:47:24 already and that's why the health level
  • 00:47:27 right here keeps decreasing as you can
  • 00:47:29 see up here besides that we also have to
  • 00:47:33 issue that only the monster health keeps
  • 00:47:37 decreasing
  • 00:47:38 that's simply due to the fact that we
  • 00:47:40 never get to execute this code right
  • 00:47:42 here because what's basically happening
  • 00:47:43 is at the moment that we have the
  • 00:47:46 situation that the monster health is
  • 00:47:48 smaller than or equal to zero well and
  • 00:47:50 this means the code rodriram is no
  • 00:47:53 longer executed so we've each round we
  • 00:47:54 start again our while loop then we come
  • 00:47:56 right here to if monster health is
  • 00:47:58 smaller than or equal to zero well this
  • 00:48:00 is the case
  • 00:48:01 therefore we go down here to if player
  • 00:48:03 one is true or monster one is true in
  • 00:48:05 our case the player has won something
  • 00:48:07 you run this false and so on so we
  • 00:48:09 basically skip this second else block
  • 00:48:12 right here this will be automatically
  • 00:48:14 fixed if we implement the logic
  • 00:48:16 correctly now now as I said the problem
  • 00:48:19 is that we don't reset the values and to
  • 00:48:21 easily fix that we can simply take this
  • 00:48:23 code right here and put it into our
  • 00:48:27 first while loop right here with the
  • 00:48:29 correct indentation of course with that
  • 00:48:32 we make sure that as soon as we leave
  • 00:48:34 our while loop right here we go back to
  • 00:48:37 the game running and initialize our
  • 00:48:39 variables with these default values now
  • 00:48:42 let's see if this works now if we now
  • 00:48:45 run the code hit attack multiple times
  • 00:48:48 to see that the monster should be dead
  • 00:48:51 now now it is not it's attack
  • 00:48:54 and as you can see now the monster is
  • 00:48:57 back at 87 after first round
  • 00:49:00 this means the player made one attack if
  • 00:49:02 I continue doing this you can see now
  • 00:49:04 the monster is dead – for now we have
  • 00:49:08 another round and the values are reset
  • 00:49:10 it like that and the monster again has
  • 00:49:12 only a health of 87 so with the new
  • 00:49:16 structure we basically control the
  • 00:49:19 entire game logic only with these two
  • 00:49:22 while loops so this one right here and
  • 00:49:23 this one right there the only problem we
  • 00:49:26 have now is that we are not able to exit
  • 00:49:28 the game but this should be an easy fix
  • 00:49:30 now right because we can simply add a
  • 00:49:32 third print statement right here
  • 00:49:35 so print free exit game like that and
  • 00:49:41 now we can simply add another Elif
  • 00:49:44 statement right here so Elif player
  • 00:49:48 choice is equal to 3 then we say oops
  • 00:49:53 not like that at the rotation mark right
  • 00:49:56 here then we can say that well what
  • 00:50:00 should happen well game running right
  • 00:50:03 here should be set to false with that we
  • 00:50:06 will also exit this value so let's say
  • 00:50:09 game running should be equal to false
  • 00:50:11 right here now let's check that let's
  • 00:50:13 leave the game clear it and run the code
  • 00:50:17 so attack it's working and if I hit free
  • 00:50:19 now and exit the game hmm this is not
  • 00:50:22 working do you know why
  • 00:50:25 well of course we are setting game
  • 00:50:27 running two folds but we are still right
  • 00:50:29 here in our new round loop so we of
  • 00:50:32 course also have to change our new round
  • 00:50:36 to false with that if we run our code
  • 00:50:40 once again oops
  • 00:50:42 like that like that attack works exit
  • 00:50:47 game works now we are out of our game is
  • 00:50:50 it again we can start it and exit it so
  • 00:50:53 this is also working fine now we are
  • 00:50:56 making a good progress with the game in
  • 00:50:58 general I would say I would like to work
  • 00:51:01 on two things now though the first thing
  • 00:51:03 is I would like to add another input
  • 00:51:05 field because at the
  • 00:51:08 moment we have a fixed value for the
  • 00:51:10 name key for player and I think it would
  • 00:51:13 be nicer if we can add our name
  • 00:51:15 individually right here we could also
  • 00:51:17 change this for the monster but I think
  • 00:51:19 the fixed monster name is fine right
  • 00:51:21 here as a second step I would like to
  • 00:51:23 make our game a bit more beautiful
  • 00:51:25 so maybe add some a print statements and
  • 00:51:27 yeah maybe make it better display the
  • 00:51:29 health information more often things
  • 00:51:32 like that but let's start with that
  • 00:51:34 additional input right here because when
  • 00:51:37 we start the game I would like to be
  • 00:51:39 able to enter the player name now to do
  • 00:51:43 that we can simply arm add another print
  • 00:51:47 method right here print function and
  • 00:51:49 this could be something like enter
  • 00:51:51 player name right here now with that we
  • 00:51:53 print that but of course we also want to
  • 00:51:55 be able to change the player name
  • 00:51:58 according to our input now for that we
  • 00:52:01 need to access the name key once again
  • 00:52:04 and change the value so why don't we
  • 00:52:05 just set player name right here equal to
  • 00:52:11 not sure what and input once again
  • 00:52:14 because the player should be able to
  • 00:52:15 enter the name on his own right here
  • 00:52:17 with that if we start our game once
  • 00:52:20 again we can see that we can enter the
  • 00:52:23 player name let's say menu right here
  • 00:52:24 and then the game starts and as we can
  • 00:52:27 see it arounds continue like that and as
  • 00:52:30 soon as the game ends yeah we can enter
  • 00:52:32 the player name once again so this is
  • 00:52:35 working fine I would like to have some
  • 00:52:37 more visual improvements now as I said a
  • 00:52:40 few seconds ago the first thing we could
  • 00:52:42 do right here is we could add an
  • 00:52:44 additional print statement on top of our
  • 00:52:46 enter player name and this print
  • 00:52:48 statement should should just be some
  • 00:52:50 dotted line you could say which
  • 00:52:52 separates the content a bit makes it
  • 00:52:54 easier to read in the end for that we
  • 00:52:57 can simply print and now we enter two
  • 00:53:00 quotation marks and now we simply add
  • 00:53:02 three lines like that so free – is
  • 00:53:05 basically now we can use another
  • 00:53:08 arithmetic operator right here this one
  • 00:53:10 right here to start to multiply these by
  • 00:53:13 a number of our choice let's maybe say 7
  • 00:53:16 or something like that this simply means
  • 00:53:18 we want to print 21 of these dashes
  • 00:53:22 um before the enter player name text is
  • 00:53:26 printed so let's look at that if we exit
  • 00:53:28 the game like this you can see this is
  • 00:53:31 this nice line right here and I think
  • 00:53:33 this improves the look of our game a bit
  • 00:53:35 we could also add such a well separator
  • 00:53:39 you could say right here before we can
  • 00:53:42 select the actual action this is just
  • 00:53:45 some cosmetic improvement of course
  • 00:53:46 nothing that is really required but I
  • 00:53:48 think it's nice to freshen up the game a
  • 00:53:50 bit with these things feel free to add
  • 00:53:52 this add additional positions I'm happy
  • 00:53:55 with that at the moment what I'm not
  • 00:53:57 happy about this is the fact that we
  • 00:54:00 don't CD health at the beginning of the
  • 00:54:02 game and also at the end of the game
  • 00:54:04 because if I select the action now you
  • 00:54:06 can see that we see the health right we
  • 00:54:09 see it right here
  • 00:54:10 but we don't have any kind of
  • 00:54:12 information regarding while the player
  • 00:54:13 health is this value and the monster
  • 00:54:16 health is that value right here so let's
  • 00:54:18 first add the health information at the
  • 00:54:21 beginning of a game and then improve the
  • 00:54:23 way the health status is displayed
  • 00:54:25 throughout the game now at the beginning
  • 00:54:27 of the game means that we have to go up
  • 00:54:30 here so once the game is started up so
  • 00:54:33 we can say that after the player entered
  • 00:54:37 his name we want to print right here our
  • 00:54:40 player name right here and now we want
  • 00:54:44 you at the information about something
  • 00:54:46 like the player so manual or whatever
  • 00:54:49 name we gave him has a 100 health like
  • 00:54:53 that but 100 health should be retrieved
  • 00:54:56 from the dictionary right here from the
  • 00:55:00 player and 100 health for the monster
  • 00:55:03 should be retrieved from this dictionary
  • 00:55:04 because we could also change our health
  • 00:55:06 to 200 or something like that when the
  • 00:55:08 game starts so I want to make sure that
  • 00:55:10 this is displayed at the beginning and
  • 00:55:11 that the player has the information
  • 00:55:12 about the health amounts at the
  • 00:55:14 beginning of the game for that we have
  • 00:55:17 to combine different types of
  • 00:55:19 information right here the first thing
  • 00:55:21 is right here the value from the
  • 00:55:23 dictionary this will return a string so
  • 00:55:26 this should be fine right here but if we
  • 00:55:27 now add a plus sign like that we can
  • 00:55:30 combine a second information that should
  • 00:55:33 be printed right here now as I said if a
  • 00:55:35 to print the player name has a specific
  • 00:55:38 amount of health then we could add
  • 00:55:40 something like has right here so an
  • 00:55:42 ordinary string we can also add space at
  • 00:55:45 the beginning and at the end because
  • 00:55:46 this will make sure that the player name
  • 00:55:48 and has it's not connected directly but
  • 00:55:50 that we have some space in between these
  • 00:55:53 two values that should be displayed
  • 00:55:55 right here after that we can add another
  • 00:55:57 + to add our player health right we
  • 00:56:01 learn how this works so the player
  • 00:56:03 health should be displayed like that so
  • 00:56:05 with that we can now access the health
  • 00:56:07 value of the health key with the
  • 00:56:10 corresponding value of 100 right here
  • 00:56:11 the problem though is that if we would
  • 00:56:14 execute it like this we will get an
  • 00:56:16 error let me show you this if I start
  • 00:56:18 the game once again and the name then
  • 00:56:21 you can see that we get an error that we
  • 00:56:23 can only concatenate strings not
  • 00:56:25 integers the issue is that player name
  • 00:56:28 right here now print statement is a
  • 00:56:30 string it's manual right here or if we
  • 00:56:32 change it it's the input that the player
  • 00:56:34 gave us so menu in this case has isn't
  • 00:56:37 is a string we can see it right here but
  • 00:56:39 player health is an integer and this
  • 00:56:42 causes a problem here because well as we
  • 00:56:44 said we cannot concatenate strings and
  • 00:56:46 integers there is one easy solution for
  • 00:56:49 that issue though we can simply use the
  • 00:56:51 so called string method in Python to
  • 00:56:53 turn our value right here into a string
  • 00:56:57 now how does this work we simply add STR
  • 00:57:00 in front of the value that should be
  • 00:57:03 kind of converted to a string you could
  • 00:57:05 say now we open the brackets right here
  • 00:57:08 and we are done because as you can see
  • 00:57:11 right here now we basically well kind of
  • 00:57:14 wrapped our player health into this
  • 00:57:19 string method and with that it should be
  • 00:57:21 turned into a string we'll see in a few
  • 00:57:23 seconds if this works let's just add one
  • 00:57:25 last item right here to our print
  • 00:57:29 statement and this simply is well
  • 00:57:31 another string with a space at the
  • 00:57:32 beginning health because like that
  • 00:57:35 health because I just want to print
  • 00:57:36 player name so whatever name we give the
  • 00:57:38 player has 100 so this value health
  • 00:57:41 let's see if this works let's save it
  • 00:57:43 and let's run the code once again let's
  • 00:57:45 enter menu and as you can see we print
  • 00:57:48 menu
  • 00:57:49 has 100 health right here on top now
  • 00:57:52 this works fine so let's also do the
  • 00:57:54 same thing for our monster right here
  • 00:57:56 here we simply have to change the
  • 00:57:59 monster name right here like this and
  • 00:58:03 this should be the monster health
  • 00:58:06 important to keep in mind player name
  • 00:58:09 right here refers to the variable right
  • 00:58:11 there because we gave the variable a
  • 00:58:13 different information right here so we
  • 00:58:15 said that the player name is equal to
  • 00:58:17 our input the monster name right here is
  • 00:58:20 still equal to the value we have for the
  • 00:58:22 name key on top right here in all
  • 00:58:24 monster variable with that if we quickly
  • 00:58:27 exit our game run it once again and the
  • 00:58:31 name we see that we have menu with 100
  • 00:58:36 health and Max with 100 health too we
  • 00:58:39 could also add to make it a bit more
  • 00:58:41 readable the print statement right here
  • 00:58:44 again so that we have the separation so
  • 00:58:47 if that we made the startup a lot more
  • 00:58:49 beautiful let's not have a look at the
  • 00:58:51 content we print throughout the actual
  • 00:58:54 round while it's running because right
  • 00:58:56 here ice is something that I don't like
  • 00:58:58 these to print methods right here
  • 00:59:00 because our if statement right here
  • 00:59:02 should actually just contain the logic
  • 00:59:04 to see how much health impact the player
  • 00:59:07 attack has and then to see if the
  • 00:59:09 counterparty saw the monster or a player
  • 00:59:11 right here is still alive of course we
  • 00:59:13 also want to make sure that we kind of
  • 00:59:15 show the impact of the monster attack
  • 00:59:17 but these to print methods no I don't
  • 00:59:20 think this should be placed right here
  • 00:59:21 so let's delete it and let's not think
  • 00:59:24 about a better location for these
  • 00:59:25 methods well I think about a location
  • 00:59:28 would be down here because here we have
  • 00:59:31 the actual selections but after these
  • 00:59:33 selections are checked so after we know
  • 00:59:36 which choice the player made then we can
  • 00:59:38 add these statements and for that we
  • 00:59:40 should refactor this if statement right
  • 00:59:43 here – because what is actually
  • 00:59:44 happening
  • 00:59:45 well if player 1 right here is equal to
  • 00:59:52 false and now we use the end logical
  • 00:59:57 operator and if monster 1
  • 01:00:01 right here is also false so basically
  • 01:00:04 this means both the player and the
  • 01:00:06 monster are still alive then please
  • 01:00:08 print something like player name has
  • 01:00:16 player health – same logic we used above
  • 01:00:21 already left but what do we have to do
  • 01:00:25 right here we have to put the left into
  • 01:00:27 quotation marks and data space and we
  • 01:00:30 have to combine these different values
  • 01:00:32 we want to output with a plus right here
  • 01:00:34 and right there
  • 01:00:36 the same thing could be printed for our
  • 01:00:38 monster of course so we could say the
  • 01:00:42 monster right here also has a specific
  • 01:00:45 amount of health left by the way
  • 01:00:48 selecting these two items at once simply
  • 01:00:51 works by pressing command D on the Mac
  • 01:00:53 right here here we have an issue still
  • 01:00:55 because I overlooked this has so we
  • 01:00:57 should select both of these and now put
  • 01:00:59 it into sink rotation marks and add a
  • 01:01:02 space at the beginning and at the end of
  • 01:01:04 it now this looks better and we should
  • 01:01:07 add the plus right here of course like
  • 01:01:08 this and like that now we don't have an
  • 01:01:11 error here anymore but we should also be
  • 01:01:13 factored this code down here now because
  • 01:01:15 if nobody won well then this code will
  • 01:01:18 run but L if so else if the player won
  • 01:01:26 by the way we can also get rid of this
  • 01:01:29 truth statement right here because if
  • 01:01:31 you just write like player 1 then this
  • 01:01:33 automatically means that if player 1 is
  • 01:01:35 true so basically writing this player 1
  • 01:01:40 equals 2 is the same thing as writing if
  • 01:01:43 player 1 so if player 1 we could say we
  • 01:01:47 also print something like um player name
  • 01:01:53 plus 1 and of course new round is set to
  • 01:01:59 false then that is to correct and
  • 01:02:02 another Elif right here if the monster 1
  • 01:02:07 like that new round is also set to false
  • 01:02:11 and we should print right here oh maybe
  • 01:02:15 not
  • 01:02:16 refer to the player to monster name we
  • 01:02:18 could simply type something like the
  • 01:02:21 monster one like this right here now if
  • 01:02:27 that we refactor our code a bit we are
  • 01:02:29 now able to print different statements
  • 01:02:32 depending on whether the player has one
  • 01:02:34 or the monster has one and we also print
  • 01:02:37 the current health statues if nobody won
  • 01:02:40 so if the round basically continues one
  • 01:02:42 thing we have to change your dough is we
  • 01:02:44 have to turn our health right here into
  • 01:02:47 a string again that's also the same
  • 01:02:49 issue we had above but we also edit the
  • 01:02:51 print statement so let's add a string
  • 01:02:53 method right here and right there like
  • 01:02:57 this so if we now restart the game it
  • 01:03:02 here enter the name attack we can see
  • 01:03:05 maybe let's crease a little bit we can
  • 01:03:09 see man us 88 left so it decreases it
  • 01:03:13 keeps decreasing and now we see that
  • 01:03:15 menu 1 because the game was over as we
  • 01:03:18 can see the monster so max only had 9
  • 01:03:21 left and we see that I won the game in
  • 01:03:24 this case so this is working final so
  • 01:03:27 and with that we improved the structure
  • 01:03:29 of our game a lot as I would say a good
  • 01:03:31 next step now would be to also work on
  • 01:03:34 the other if statements specifically I
  • 01:03:37 would like to work on the heal option
  • 01:03:38 right here because at the moment we can
  • 01:03:40 basically just attack or exit the game
  • 01:03:43 but heal player still is simply not just
  • 01:03:46 a print statement so let's work on that
  • 01:03:48 now if our player says that he just
  • 01:03:51 wants to heal himself and not attack the
  • 01:03:53 monster then the logic is a bit
  • 01:03:55 different because what will happen then
  • 01:03:57 well we can simply say that if the
  • 01:03:59 player wants to heal himself then player
  • 01:04:02 health right here so we again access the
  • 01:04:05 health key of our player is a quote you
  • 01:04:08 well the player health that he currently
  • 01:04:11 has but now not minus something because
  • 01:04:15 we don't want to subtract something we
  • 01:04:18 want to add right here the heal amount
  • 01:04:21 we defined for our player just to bring
  • 01:04:23 that back to your mind the heal amount
  • 01:04:25 is this key right here so we have a heal
  • 01:04:27 key with a value of 16
  • 01:04:29 so let's add this now so we can say that
  • 01:04:32 playerhealth is equal to the current
  • 01:04:34 playerhealth plus the player heel that
  • 01:04:38 we have right here what we don't need
  • 01:04:41 here is now we don't need to check
  • 01:04:43 whether our player is still alive
  • 01:04:45 because well if we survived if the
  • 01:04:47 previous round then you will be able to
  • 01:04:48 heal himself because of that the health
  • 01:04:50 will increase but after the player
  • 01:04:54 healed himself well the monster will
  • 01:04:56 still be able to attack the player so we
  • 01:04:59 have to add this monster attack now to
  • 01:05:02 do that we can simply say that player
  • 01:05:04 health now but this is now the updated
  • 01:05:06 player health so this one right here so
  • 01:05:08 player health is equal to well the
  • 01:05:14 player health again so the current value
  • 01:05:16 for the player health – the monster
  • 01:05:21 attack that we have so basically it's
  • 01:05:25 the same logic that we had right here
  • 01:05:28 however after this attack we have to
  • 01:05:30 check whether our players still alive so
  • 01:05:33 we can again copy that code right here
  • 01:05:35 so if the player health is below 0 or
  • 01:05:38 equal to 0 then well unfortunately our
  • 01:05:41 player has lost the game and the monster
  • 01:05:44 has won so this would be the logic for
  • 01:05:46 our second choice right here for our
  • 01:05:49 second option let's quickly check if
  • 01:05:51 this works let me enter name and exit
  • 01:05:53 the game so let's now we start the game
  • 01:05:56 and enter name once again and let's
  • 01:06:00 attack yeah we have 88 life left so
  • 01:06:03 let's see if we kill ourselves
  • 01:06:04 we got 92 left which basically makes
  • 01:06:06 sense because we got an attack from the
  • 01:06:09 monster of 12 but we had a heal level of
  • 01:06:13 16 so we increased our health by 4 so
  • 01:06:16 this is also working fine now with that
  • 01:06:19 code added I think it's time to think
  • 01:06:21 about the core game logic once again
  • 01:06:24 because at the moment we have a pretty
  • 01:06:26 well let's say easy to predict game
  • 01:06:31 result because we only have two attack
  • 01:06:33 options for the player and monster so
  • 01:06:36 it's 13 for player and 12 for the
  • 01:06:38 monster so basically if the player
  • 01:06:40 always attacks well he will win the game
  • 01:06:43 that's pretty boring so I think we
  • 01:06:44 should make this a bit more dynamic and
  • 01:06:46 I actually don't want to change the
  • 01:06:48 player values right here I think these
  • 01:06:51 are okay but I'm not happy with the
  • 01:06:53 monster right here because wouldn't it
  • 01:06:55 be great if we could make this a bit
  • 01:06:56 more dynamic if we define a minimum
  • 01:06:59 value for the monster attack and a
  • 01:07:02 maximum value and then kind of create
  • 01:07:04 something which allows us to dynamically
  • 01:07:06 select the value in between this minimum
  • 01:07:09 and maximum with that we have a random
  • 01:07:11 element in our game and this would make
  • 01:07:13 it a lot more exciting now how can we
  • 01:07:16 achieve that well the first thing is we
  • 01:07:18 should get rid of that single key right
  • 01:07:22 here and add two keys instead of this
  • 01:07:24 one one could be attack min right here
  • 01:07:28 for the minimum attack value which could
  • 01:07:31 be equal to let's say 12 and then we add
  • 01:07:34 another value or another key and value
  • 01:07:36 which could be attack max right here
  • 01:07:40 which could have a value of well let's
  • 01:07:44 say 20 maybe even this can make it a
  • 01:07:46 little bit more exciting let's maybe
  • 01:07:47 decrease this a bit to 10 let's say
  • 01:07:49 something like that with that we now
  • 01:07:52 have these two options the problem is
  • 01:07:55 that we now need a way to well kind of
  • 01:07:56 choose a value in between 10 and 20
  • 01:07:58 randomly for each round for that purpose
  • 01:08:02 we need to import a so called module
  • 01:08:04 chip – now what is a module generally we
  • 01:08:08 could write everything from scratch
  • 01:08:10 right here in Python this is generally
  • 01:08:12 possible but isn't really convenient and
  • 01:08:14 it would mean that we have to write a
  • 01:08:15 lot of code and a lot of logic just for
  • 01:08:18 things like while creating such a random
  • 01:08:20 number in the end because of that –
  • 01:08:22 comes with a lot of different modules
  • 01:08:25 which can be imported to your project
  • 01:08:27 and which come with these features
  • 01:08:29 implemented by default but the important
  • 01:08:32 thing is that you can use different
  • 01:08:33 features of this module in a very very
  • 01:08:37 convenient way now let's see how this
  • 01:08:39 works now because I think then it will
  • 01:08:41 become a lot clearer you basically
  • 01:08:43 import a module to – by simply going to
  • 01:08:46 the top of your code right here and then
  • 01:08:49 import the module of your choice in our
  • 01:08:51 case we need to import from the
  • 01:08:54 so-called random
  • 01:08:56 jool more about modules can be found in
  • 01:08:59 the link in the video description by the
  • 01:09:01 way and from this random module we want
  • 01:09:04 to import a specific feature you could
  • 01:09:06 say in our case we want to import the
  • 01:09:09 rant int feature this one right here the
  • 01:09:12 rant in feature simply allows us to
  • 01:09:15 return a random integer between a bottom
  • 01:09:18 and a top corridor you could say so in
  • 01:09:22 our case between the minimum attack and
  • 01:09:24 the maximum attack and the cool thing
  • 01:09:26 about this module now is that we can
  • 01:09:28 simply use this feature without writing
  • 01:09:30 any additional code we only have to use
  • 01:09:32 a specific method as I said methods and
  • 01:09:35 functions or something we'll cover in a
  • 01:09:37 few seconds and then we will get back a
  • 01:09:39 random number now a lot of talking how
  • 01:09:42 does this work now well basically we
  • 01:09:45 want to have our monster attack down
  • 01:09:47 here so this basically this monster
  • 01:09:49 attack is now no longer a fixed value
  • 01:09:51 but this should be a random value for
  • 01:09:54 that we can simply define a variable
  • 01:09:57 let's say monster attack really
  • 01:10:01 important this now is a variable you can
  • 01:10:04 see this with the underscore right here
  • 01:10:06 and I don't refer to the monster attack
  • 01:10:08 key right here right that's important so
  • 01:10:11 I'm not referring to any kind of attack
  • 01:10:13 key which by the way is also not
  • 01:10:15 existing any longer and I want to store
  • 01:10:17 an information a specific information in
  • 01:10:20 it variable and this information L is
  • 01:10:23 something that is only available because
  • 01:10:25 we imported this random module I will
  • 01:10:29 now use a so called function or a method
  • 01:10:31 and we will dive deeper into functions
  • 01:10:34 in a few minutes so just to give you a
  • 01:10:35 big picture a function is simply some
  • 01:10:39 code that can be executed whenever we
  • 01:10:42 call this function so whenever we tell
  • 01:10:44 Python to well refer to that function
  • 01:10:47 you could say then this code will be
  • 01:10:49 executed and the Rand int is a function
  • 01:10:52 which is available due to the random
  • 01:10:54 module be imported so we don't see the
  • 01:10:56 actual code inside that function right
  • 01:10:58 here but we have this function or method
  • 01:11:01 available now as we use this random
  • 01:11:04 module a function is called by typing
  • 01:11:07 the function name and adding the
  • 01:11:09 parentheses right here
  • 01:11:10 so adding the parentheses simply means
  • 01:11:12 that you call the function we have two
  • 01:11:14 types of functions though all to
  • 01:11:16 something we'll have a look at later by
  • 01:11:17 the way we have functions with empty bar
  • 01:11:19 and parentheses this would be something
  • 01:11:21 like this or in the case of the rent in
  • 01:11:23 function we have a function which
  • 01:11:25 requires so-called parameters or
  • 01:11:28 arguments if a function requires such
  • 01:11:32 arguments this simply means that we have
  • 01:11:34 to pass some additional information from
  • 01:11:37 outside to this function to make sure
  • 01:11:40 the code inside the functions can be
  • 01:11:43 executed correctly and the information
  • 01:11:45 we pass to this function our so-called
  • 01:11:47 arguments we will have a look at
  • 01:11:48 parameters and arguments also later in
  • 01:11:50 more detail so the important thing right
  • 01:11:52 here is we have this rent in function so
  • 01:11:55 this predefined code we can execute
  • 01:11:58 whenever we need it and we have to add
  • 01:12:00 additional information to this function
  • 01:12:02 to make sure the code can be executed
  • 01:12:04 correctly now as we want to calculate a
  • 01:12:07 random integer we need to add two
  • 01:12:09 arguments here the first one is the
  • 01:12:12 minimum value for our attack so we need
  • 01:12:14 monster attack min right here and the
  • 01:12:17 second argument is the monster attack
  • 01:12:19 max that's the second argument and with
  • 01:12:21 that we are now able to call this
  • 01:12:24 function inside our variable and with
  • 01:12:26 that to retrieve the calculated random
  • 01:12:29 number for the monster attack with these
  • 01:12:32 two arguments added we now made sure
  • 01:12:34 that if we refer to the monster attack
  • 01:12:36 variable this function is called and
  • 01:12:38 executed and therefore we only have to
  • 01:12:40 change our monster attack right here
  • 01:12:43 with D monster
  • 01:12:44 underscore attack variable because if we
  • 01:12:46 now subtract this monster attack
  • 01:12:48 variable we will subtract the result of
  • 01:12:51 the Rand int function and this will be
  • 01:12:53 this random attack value calculated due
  • 01:12:57 to our random module so this was a lot
  • 01:12:59 of talking let's now see if this works
  • 01:13:01 so let's exit the game and start it once
  • 01:13:03 again and player name and let's attack
  • 01:13:07 the monster so we see menu has 83 left
  • 01:13:11 now so we kind of deducted 17 let's try
  • 01:13:15 it another time now we have 65 left so
  • 01:13:18 83 minus 1818 is 65 so another random
  • 01:13:22 number
  • 01:13:23 now we have 50 left so 15 so basically
  • 01:13:27 this seems to work now if this random
  • 01:13:29 module right here we are able to create
  • 01:13:31 this random integer now we simply have
  • 01:13:33 to also copy that code from here to down
  • 01:13:37 there to our second choice if we decide
  • 01:13:39 to heal ourselves and let's make sure
  • 01:13:42 the indentation is correct right here
  • 01:13:46 like this and like that so this is not
  • 01:13:51 working fine but we just saw something
  • 01:13:53 that you should avoid no matter if
  • 01:13:55 you're writing Python code or any other
  • 01:13:57 code we just copy and paste it well
  • 01:14:01 basically the same code right that's not
  • 01:14:04 always a totally bad thing but
  • 01:14:05 especially if we have things like this
  • 01:14:07 monster attack right here so this line
  • 01:14:09 right here and right there this is not
  • 01:14:12 really a good
  • 01:14:13 well code structure I would say because
  • 01:14:16 what is actually happening right here is
  • 01:14:17 we only need this code to get this
  • 01:14:20 random integer now wouldn't it be better
  • 01:14:23 to kind of outsource this code and to
  • 01:14:26 kind of to be able to refer to the
  • 01:14:27 result of this calculation right here in
  • 01:14:30 monster attack at the moment this is not
  • 01:14:32 possible because we first have to define
  • 01:14:34 or declare the variable to get back this
  • 01:14:37 value situations like that are the
  • 01:14:40 perfect area or application area for
  • 01:14:43 so-called functions now what are
  • 01:14:45 functions functions basically allow us
  • 01:14:48 to run a specific predefined code
  • 01:14:52 whenever we call such a function calling
  • 01:14:55 a function simply means that we tell the
  • 01:14:58 function that it should be executed now
  • 01:15:00 now how do we write such functions let's
  • 01:15:03 go down to the code and create such a
  • 01:15:05 function a function in Python is created
  • 01:15:07 by simply typing the death keyword right
  • 01:15:10 here now we have to give the function a
  • 01:15:12 name and the name should basically
  • 01:15:14 describe what is well basically done
  • 01:15:17 inside the function so in our case what
  • 01:15:19 are we calculating and this could be
  • 01:15:21 something like monster attack maybe like
  • 01:15:25 this the problem is that we have monster
  • 01:15:27 attack already right here for our
  • 01:15:29 variable and this is something you
  • 01:15:31 should avoid you should not name
  • 01:15:33 variables and functions equal that's
  • 01:15:35 something you should not do they have
  • 01:15:37 or we will just change the name here to
  • 01:15:39 monster attack or maybe let's call it
  • 01:15:45 calculate armed monster attack like that
  • 01:15:49 with that we have a different naming
  • 01:15:51 right here then we add parentheses right
  • 01:15:53 there
  • 01:15:53 and then we add a colon and here we now
  • 01:15:55 have a function which doesn't require
  • 01:15:57 any arguments you remember when we used
  • 01:16:00 to rent in function before we had to add
  • 01:16:02 the attack min and attack maximum as
  • 01:16:04 arguments otherwise the function
  • 01:16:06 wouldn't work correctly in this case we
  • 01:16:09 don't need that arguments because the
  • 01:16:11 function doesn't require any additional
  • 01:16:13 information from outside and with that
  • 01:16:16 we're basically back to our already
  • 01:16:17 known Python programming syntax because
  • 01:16:20 now we hit enter and now the indented
  • 01:16:23 code right here will only be executed if
  • 01:16:25 we call so if we basically execute the
  • 01:16:29 function right here now what you want to
  • 01:16:31 do is if we refer to this function we
  • 01:16:34 just want to have this random integer so
  • 01:16:37 we can just refer to this code right
  • 01:16:38 here copy it and paste it right there
  • 01:16:42 into our function so let's now see what
  • 01:16:45 happens if we save that right here we
  • 01:16:47 can see it we get no errors so this
  • 01:16:50 seems to work ok so let's exit the game
  • 01:16:52 and let's now start the game once again
  • 01:16:55 and rename attack well this works fine
  • 01:16:59 of course but the problem is that we
  • 01:17:01 basically don't use the function right
  • 01:17:03 now because as I said before we have to
  • 01:17:06 call a function to really execute the
  • 01:17:09 code that we saved right here what does
  • 01:17:11 this mean for us well we basically
  • 01:17:13 should get rid right here in our first
  • 01:17:16 if statement of the monster attack a
  • 01:17:20 variable we don't need this variable
  • 01:17:22 anymore because the only purpose of this
  • 01:17:24 variable was to basically calculate this
  • 01:17:26 random number this is now done by our
  • 01:17:28 function and we can instead of that now
  • 01:17:32 say that we just refer to the calculate
  • 01:17:38 monster attack function and important to
  • 01:17:41 call a function in Python we have to add
  • 01:17:44 the parentheses right here let's now
  • 01:17:46 save this and let's now see what is
  • 01:17:48 happening so if we know X
  • 01:17:51 the game and her name menu like that say
  • 01:17:57 attack then we get an error it says that
  • 01:18:02 the calculate monster attack right here
  • 01:18:04 this name is not defined but that's
  • 01:18:07 strange right
  • 01:18:08 because actually we defined it down
  • 01:18:10 there but somehow puffin doesn't know
  • 01:18:13 that this function exists and therefore
  • 01:18:15 has no idea what it should do right
  • 01:18:17 there now this is also very important to
  • 01:18:20 understand the code right here in Python
  • 01:18:23 and generally in programming actually is
  • 01:18:25 always read or parsed from top to bottom
  • 01:18:28 this means if we think about what is pi
  • 01:18:31 for doing right here it starts reading
  • 01:18:33 the code up there scans the code scans
  • 01:18:36 the code scans the code and then it sees
  • 01:18:38 okay we want to execute or call a
  • 01:18:41 function right here but the problem is
  • 01:18:43 that Python has no idea that this
  • 01:18:45 function is defined down there because
  • 01:18:47 as I said the code is parsed from top to
  • 01:18:49 bottom
  • 01:18:50 therefore how should it know that
  • 01:18:52 therefore we will take this function l
  • 01:18:54 scroll to the top of our code and add it
  • 01:18:57 right here ahead of our first Y loop
  • 01:19:00 with that pipe now understands that we
  • 01:19:03 have a function named calculate monster
  • 01:19:05 attack and that this function will be
  • 01:19:08 called right here now let's see what
  • 01:19:10 happens now if you run the game once
  • 01:19:11 again enter the player name menu right
  • 01:19:14 here and attack now we have another
  • 01:19:17 problem
  • 01:19:17 Python is aware of our function but we
  • 01:19:20 basically have something like an
  • 01:19:22 unsupported operand type the coughing
  • 01:19:26 right here is this non type because our
  • 01:19:29 function is not returning anything at
  • 01:19:32 the moment because we basically told our
  • 01:19:34 function up here that it should
  • 01:19:36 calculate the random integer but the
  • 01:19:38 problem is we didn't tell the function
  • 01:19:39 what it should do then so it calculates
  • 01:19:41 that integer but well then that's it
  • 01:19:44 therefore we have to add another
  • 01:19:46 important statement to this function
  • 01:19:48 right here this so-called return
  • 01:19:50 statement like this return simply tells
  • 01:19:54 python that it should well return the
  • 01:19:57 result of this operation and the result
  • 01:19:59 of this operation is the calculated
  • 01:20:01 value for our attack so if we save that
  • 01:20:04 now
  • 01:20:05 and run the code once again enter the
  • 01:20:07 player name and attack then you can see
  • 01:20:10 that this is working now because now
  • 01:20:13 basically what happens is we say that we
  • 01:20:17 call the function right here we
  • 01:20:19 calculate the value for our monster
  • 01:20:22 attack and we return that value and with
  • 01:20:25 that we insert the value right here in
  • 01:20:27 our function and therefore T
  • 01:20:29 playerhealth is recalculated what we can
  • 01:20:32 do now of course is we can also take
  • 01:20:34 that code
  • 01:20:35 so this playerhealth we just created and
  • 01:20:37 also replace it for our second if case
  • 01:20:40 right here important make sure to do the
  • 01:20:44 indentation indentation correctly too so
  • 01:20:48 besides the fact that we now refactor
  • 01:20:51 our code again made it a bit leaner
  • 01:20:53 which is always good we also had a first
  • 01:20:56 look at functions and to be more precise
  • 01:20:59 we actually solve two types of functions
  • 01:21:01 already let's scroll up a bit to the
  • 01:21:04 functions we use because we have our
  • 01:21:06 calculate monster attack function and we
  • 01:21:08 have our rent in red here so the
  • 01:21:10 function we use from our random module
  • 01:21:12 that we imported and if you look at the
  • 01:21:15 parenthesis at the end of these two
  • 01:21:16 functions we see a big difference the
  • 01:21:19 first function right here
  • 01:21:20 has empty parentheses this means this
  • 01:21:23 function doesn't require any arguments
  • 01:21:26 or parameters the rent in function on
  • 01:21:29 the other hand requires two arguments in
  • 01:21:31 our case monster attack min and monster
  • 01:21:33 attack max what's the difference here
  • 01:21:35 well the calculate monster attack
  • 01:21:37 function doesn't need any kind of
  • 01:21:40 external information to run correctly
  • 01:21:42 because if we call that function we
  • 01:21:45 simply execute this code right here and
  • 01:21:46 this code comes with all the information
  • 01:21:49 needed well to run the function this
  • 01:21:51 means we don't need any parameters right
  • 01:21:53 here we don't have to pass any arguments
  • 01:21:56 to that function so any external
  • 01:21:58 information you could say this is
  • 01:22:00 different for our random integer
  • 01:22:02 function right here we need to calculate
  • 01:22:04 a random integer between a minimum and a
  • 01:22:06 maximum value in this minimum and
  • 01:22:09 maximum value in our case is some
  • 01:22:12 external information and to pass this
  • 01:22:14 external information into our function
  • 01:22:17 we use a so called
  • 01:22:19 argument in our case we have two
  • 01:22:20 arguments we have the minimum attack and
  • 01:22:22 de maximum attack we don't have to use
  • 01:22:25 these external information as arguments
  • 01:22:28 here by the way we could also use fixed
  • 01:22:30 values for example 10 as the first
  • 01:22:32 argument and for the max value as the
  • 01:22:34 second argument we could use 20 that
  • 01:22:36 would also be possible 20 like that this
  • 01:22:39 would also be possible but this would
  • 01:22:41 make our code a lot less dynamic because
  • 01:22:45 actually the monster variable right here
  • 01:22:48 and the corresponing dictionary contains
  • 01:22:50 information about the minimum and
  • 01:22:51 maximum attack so instead of using fixed
  • 01:22:54 values as arguments right here we can
  • 01:22:57 simply pass attack min and attack max as
  • 01:23:00 arguments for the first parameter so
  • 01:23:03 this is monster attack min and the
  • 01:23:10 second parameter receives the monster
  • 01:23:14 attack max as an argument so that's the
  • 01:23:18 core idea behind functions parameters
  • 01:23:21 and arguments parameters simply allow us
  • 01:23:24 to add additional information to
  • 01:23:27 functions to make sure the code inside
  • 01:23:29 the function works correctly and the
  • 01:23:31 information we pass to these parameters
  • 01:23:33 are our so called arguments so attackman
  • 01:23:36 and attack max in our case however using
  • 01:23:40 the function like this is working fine
  • 01:23:41 in this case but this is not a perfect
  • 01:23:45 solution to be honest because at the
  • 01:23:48 moment we declare our variable some
  • 01:23:50 monster right here and we refer to this
  • 01:23:52 variable inside our function and after
  • 01:23:55 we declare the variable we call the
  • 01:23:57 function down here calculate monster
  • 01:23:59 attack but there can be situations where
  • 01:24:03 this can cause problems because what if
  • 01:24:05 we want to call our function before we
  • 01:24:09 declare the actual variable for example
  • 01:24:11 we could print our calculate monster
  • 01:24:13 attack function right here if we run our
  • 01:24:16 code now we get an error because the
  • 01:24:19 named monster is not defined because
  • 01:24:22 what's happening is that well
  • 01:24:24 – parses the code it reads the code and
  • 01:24:27 finds the function then goes into our
  • 01:24:29 while loop and then we want to print the
  • 01:24:32 function
  • 01:24:32 and then we have a problem because – is
  • 01:24:36 no idea that this monster variable
  • 01:24:38 exists because we use it in the function
  • 01:24:40 but we declare it after we call the
  • 01:24:43 function therefore this approach is not
  • 01:24:46 perfect here we can stick to it for the
  • 01:24:48 moment though but actually we should
  • 01:24:50 improve that code to make sure we avoid
  • 01:24:52 errors like that
  • 01:24:54 however we will work on that as soon as
  • 01:24:56 we were diving deeper into parameters
  • 01:24:58 and arguments because an argument might
  • 01:25:00 be helpful here nevertheless if we
  • 01:25:03 delete it right here and run the code
  • 01:25:04 again we can see this still works fine
  • 01:25:07 so we'll stick to that solution for the
  • 01:25:09 moment now why don't we practice that a
  • 01:25:12 bit and create another function which
  • 01:25:14 also requires an argument well for that
  • 01:25:17 we can scroll down and here in our game
  • 01:25:20 ending or around ending situations where
  • 01:25:23 either the player one or the monster one
  • 01:25:25 I think it would be a nice place to add
  • 01:25:26 such a function because we could
  • 01:25:28 actually create a function that might be
  • 01:25:31 called something like arm game ends
  • 01:25:33 something like this which gets called if
  • 01:25:37 the player or the monster one so we can
  • 01:25:40 actually delete this print statement
  • 01:25:42 right here and also write there and add
  • 01:25:48 game ends right here like this as you
  • 01:25:51 can see our 30 parentheses so like this
  • 01:25:53 we call this function and we don't pass
  • 01:25:56 any argument we of course also have to
  • 01:25:59 define the function trait the function
  • 01:26:01 so let's go up here again now we'll
  • 01:26:04 create the game and function without any
  • 01:26:07 arguments and say that this should print
  • 01:26:09 something like maybe um player one like
  • 01:26:16 this if we do that and run the code
  • 01:26:20 let's enter player name attack attack
  • 01:26:23 attack attack attack it seems that we
  • 01:26:27 might lose let's see ah we made it
  • 01:26:29 though we actually won the game but the
  • 01:26:32 problem still is that even if the
  • 01:26:33 monster would have won the game we would
  • 01:26:35 always print the player one because we
  • 01:26:37 always call this game and function in
  • 01:26:38 this game and function well it simply
  • 01:26:40 prints the player one so wouldn't it be
  • 01:26:42 better if we would be able to pass an
  • 01:26:45 hour
  • 01:26:46 human to this function which basically
  • 01:26:48 simply is the player name in case the
  • 01:26:51 player won right here or something like
  • 01:26:53 the monster in case the monster won for
  • 01:26:56 that we need to add a parameter to our
  • 01:26:58 function this can be done up here in the
  • 01:27:02 parenthesis and now we can specify a
  • 01:27:04 name for this parameter in our case this
  • 01:27:07 could be like the winner's name for
  • 01:27:10 example like this so we now have this
  • 01:27:13 function with this parameter and now we
  • 01:27:15 need an argument that gets passed to
  • 01:27:17 that function to be able to use it
  • 01:27:19 inside our function at the moment we
  • 01:27:21 don't use it but we will add that in a
  • 01:27:23 few seconds for that we can go back
  • 01:27:25 right here and now simply add the
  • 01:27:28 argument so the argument that gets
  • 01:27:30 passed to this parameter inside the
  • 01:27:32 parentheses right here now what is the
  • 01:27:34 argument we need in case the player won
  • 01:27:36 well it's of course the player name
  • 01:27:40 right here like that and for the monster
  • 01:27:43 well I think that's quite obvious it's
  • 01:27:45 the monster name of course this and like
  • 01:27:48 that so in case the player wins the game
  • 01:27:51 we call the game and function and we
  • 01:27:55 pass the player name as an argument this
  • 01:27:58 argument now gets passed to this
  • 01:28:01 function right here as the spinner name
  • 01:28:03 parameter important as you can see the
  • 01:28:06 naming of the argument right here and
  • 01:28:10 the parameter right there doesn't have
  • 01:28:13 to be equal can be but it doesn't have
  • 01:28:15 to but important if you want to use the
  • 01:28:18 argument then inside your function then
  • 01:28:21 you have to name it identically for
  • 01:28:24 example if you want to print the
  • 01:28:25 winner's name we have to add winner's
  • 01:28:27 name right here like this with that if
  • 01:28:32 we exit our game clear it and run it
  • 01:28:37 once again and the name menu
  • 01:28:40 and attack attack attack attack attack
  • 01:28:43 attack attack we can see max right here
  • 01:28:46 so this means the monster because this
  • 01:28:48 is the name of the monster won the game
  • 01:28:50 so this is working we now pass the
  • 01:28:53 argument to our function and can
  • 01:28:55 dynamically output the winner name the
  • 01:28:58 problem is that I might also want to
  • 01:29:00 have something like a little bit of a
  • 01:29:02 sentence right here because just
  • 01:29:03 printing the name like this
  • 01:29:04 it's not really nice as I would say for
  • 01:29:07 that something like using a string as we
  • 01:29:09 had it before
  • 01:29:10 for example player won the game like
  • 01:29:15 that would be good but player should
  • 01:29:19 actually not be the player it should be
  • 01:29:21 either to player name or if the player
  • 01:29:23 won or the monster name if the monster
  • 01:29:26 won and we have to use it dynamically
  • 01:29:28 well based on the argument we have right
  • 01:29:31 here to do this we have a really nice
  • 01:29:33 feature in – also it's called formatted
  • 01:29:37 string literals or f strings and these
  • 01:29:40 allow us to dynamically inject content
  • 01:29:42 into strings so exactly what we want to
  • 01:29:45 achieve right here to create such an F
  • 01:29:47 string you simply add well an F in front
  • 01:29:51 of your string so this is the string
  • 01:29:52 that we are using right here and you
  • 01:29:55 simply wrap the content that you want to
  • 01:29:57 dynamically inject into your string with
  • 01:29:59 curly braces so like this and like that
  • 01:30:02 this would be the player then being
  • 01:30:04 injected dynamically but as I said this
  • 01:30:07 is not working at the moment because the
  • 01:30:09 name of the argument right here and
  • 01:30:11 right there inside our function so where
  • 01:30:14 we want to use that argument has to be
  • 01:30:15 equal so with that we can simply add
  • 01:30:18 winner name right here so what is
  • 01:30:20 happening now is that if the player for
  • 01:30:22 example wins the game we pass the player
  • 01:30:24 name to our function and then we use
  • 01:30:27 this argument right here inside our f
  • 01:30:30 string if the monster wins the same
  • 01:30:34 thing happens but the winner name will
  • 01:30:35 be the monster name and not the player
  • 01:30:37 name now let's see if this works
  • 01:30:39 so let's escape our game clear it run it
  • 01:30:43 once again enter name attack attack
  • 01:30:46 attack attack attack attack and
  • 01:30:49 Tech max won the game so this is working
  • 01:30:52 fine let's not also see if we can win
  • 01:30:55 the game this so let's maybe start
  • 01:30:58 repealing ourselves healing again
  • 01:31:00 attacking the monster healing ourselves
  • 01:31:01 ah this doesn't work good healing can we
  • 01:31:05 win the game we might have to readjust
  • 01:31:06 the gaming logic a little bit I think we
  • 01:31:10 will lose again yeah now we won the game
  • 01:31:14 we did it so as you can see now we won
  • 01:31:16 the game and this is also printed now
  • 01:31:18 dynamically because now menu so the
  • 01:31:20 player name is used in our function
  • 01:31:23 let's move away from functions now and
  • 01:31:25 let's work again on the user experience
  • 01:31:27 because we have this output right here
  • 01:31:29 so monster or player on the game this is
  • 01:31:33 nice but actually I would like to add
  • 01:31:35 some more information on the player
  • 01:31:37 after a round or a game ended because
  • 01:31:40 let's say that the monster won then we
  • 01:31:43 would have max won the game right here
  • 01:31:44 but then I would also like to print the
  • 01:31:46 player info like the player named the
  • 01:31:49 player healthy had and also maybe
  • 01:31:52 something like how many rounds it took a
  • 01:31:54 single game to end so to either make
  • 01:31:56 sure to play a one or the monster one
  • 01:32:00 so for that we need to print this
  • 01:32:02 information and we need to add a counter
  • 01:32:04 let's work on it step by step and let's
  • 01:32:06 print the information first for that I
  • 01:32:09 will go down right here to our game
  • 01:32:13 ending conditions so either the player
  • 01:32:15 or the monster run we have our game and
  • 01:32:17 function and after that I would like to
  • 01:32:20 create a new variable this variable
  • 01:32:22 could be something like round result
  • 01:32:25 right here and the round result should
  • 01:32:28 contain the following information it
  • 01:32:30 should contain the name right so the
  • 01:32:34 name of our player and because of that
  • 01:32:36 you have to refer to our player name
  • 01:32:39 right here and it should contain the
  • 01:32:42 health level of our player as soon as
  • 01:32:45 the round ended and the health level is
  • 01:32:48 of course our player health right here
  • 01:32:51 now we add a print statement right here
  • 01:32:54 to print our round result and then we
  • 01:32:59 copy this information down here to our
  • 01:33:02 monster like that if you remember back I
  • 01:33:05 said that copying and pasting code is
  • 01:33:07 not always a best practice for this
  • 01:33:10 purpose here I will copy paste the code
  • 01:33:12 though because in a few minutes we will
  • 01:33:14 basically save our results and for that
  • 01:33:17 I prefer this approach I come back to
  • 01:33:20 that in a few minutes though so let's
  • 01:33:21 keep it this way for the moment if we
  • 01:33:24 now restart our game enter the name and
  • 01:33:28 attack attack attack attack attack
  • 01:33:29 attack
  • 01:33:31 you can see that menu won the game so
  • 01:33:34 basically play up on the game and then
  • 01:33:35 we save to player stats or the name and
  • 01:33:37 the health this can be helpful in case
  • 01:33:39 we have different players then each
  • 01:33:41 player can see his name and the health
  • 01:33:44 level he had now besides that I also
  • 01:33:47 wanted to add a counter so that you can
  • 01:33:50 see how many rounds it took until the
  • 01:33:51 game ended for that we simply have to
  • 01:33:54 define such a counter first and where
  • 01:33:57 should we do that well right here in our
  • 01:33:59 first while loop in our game running
  • 01:34:03 while loop because as soon as we start a
  • 01:34:05 new game not a new round a new game the
  • 01:34:08 counter has to be reset well and what
  • 01:34:11 should be the value for a counter
  • 01:34:13 nationally
  • 01:34:13 initially well it should be zero so we
  • 01:34:15 can say the counter is zero right here
  • 01:34:18 at the start of the game now whenever we
  • 01:34:23 start a new round the counter should
  • 01:34:25 increase by one because we start a new
  • 01:34:27 round the counter goes up the round
  • 01:34:28 ended the next round starts and the
  • 01:34:30 counter increases because of that we can
  • 01:34:33 simply add the counter right here in our
  • 01:34:35 second while loop so the new round while
  • 01:34:37 loop and we can again refer to our
  • 01:34:39 counter variable and say that the
  • 01:34:41 counter should be the initial value of
  • 01:34:43 the counter this one plus one because we
  • 01:34:47 increase the counter by one and as long
  • 01:34:49 as we stay inside our round the counter
  • 01:34:52 will be one when we start the round then
  • 01:34:55 we go for our while loop we stay around
  • 01:34:57 the second round starts sort of counter
  • 01:35:00 increases to do if the round ends we go
  • 01:35:02 back to our first while loop right here
  • 01:35:04 and set the counter back to zero so that
  • 01:35:07 should be the logic
  • 01:35:08 let's now also print this counter right
  • 01:35:11 here in our round result and for that we
  • 01:35:14 can simply add it as another key right
  • 01:35:17 here so this could be the round key
  • 01:35:20 right here and while the value for
  • 01:35:23 rounds is simply our counter variable
  • 01:35:25 let's also add this down here um for our
  • 01:35:32 monster one case now let's see if this
  • 01:35:35 works let's end the game and restart it
  • 01:35:40 enter a name tag attack attack attack
  • 01:35:43 attack attack and yeah as we can see max
  • 01:35:47 won the game so the monster won the game
  • 01:35:48 the player was menu the health level was
  • 01:35:51 minus seven so we were beaten and it
  • 01:35:54 took the monster seven rounds to win
  • 01:35:56 against us so if that we also
  • 01:35:58 implemented such a counter functionality
  • 01:36:00 to our game right here let's now also
  • 01:36:03 try another player name so let's say the
  • 01:36:05 other players Tony so another player
  • 01:36:08 Tony attacks attacks attacks attacks
  • 01:36:11 attacks attacks and again max won the
  • 01:36:14 game but the problem is that now Tony is
  • 01:36:17 only displayed right here but actually
  • 01:36:19 in the previous round I was playing
  • 01:36:21 against a monster but my stats were not
  • 01:36:23 safe now keep in mind that we didn't
  • 01:36:26 exit the game so we
  • 01:36:27 actually stayed inside our game but
  • 01:36:29 somehow our results were not saved this
  • 01:36:33 could be the next film we work on how
  • 01:36:35 can we save the data of a single round
  • 01:36:37 right here in our code to make sure we
  • 01:36:40 get kind of a while results list in the
  • 01:36:42 end to see how different players
  • 01:36:43 performed against the monster we
  • 01:36:47 actually learned almost everything we
  • 01:36:49 need for that already in this video
  • 01:36:51 because if you remember back the
  • 01:36:53 beginning of this video we talked about
  • 01:36:55 lists and lists allowed us to store well
  • 01:36:58 different items and actually this right
  • 01:37:01 here so this dictionary is a single item
  • 01:37:04 in the next round if another player
  • 01:37:06 plays against the monster the result
  • 01:37:08 would also be a dictionary which could
  • 01:37:10 be the next list item and so on so what
  • 01:37:13 we actually only meet is an empty list
  • 01:37:16 at the beginning of the game and to this
  • 01:37:18 empty list we then simply add the
  • 01:37:21 dictionary so this one right here every
  • 01:37:23 time a game was finished so every time
  • 01:37:26 either the monster or the player won for
  • 01:37:29 that we will first create this empty
  • 01:37:32 list and let's create that list appear
  • 01:37:34 outside of our while loop because
  • 01:37:36 basically that's the initial thing that
  • 01:37:38 should be created as soon as we run our
  • 01:37:41 Python file right here so let's maybe
  • 01:37:44 call this armed game results and they
  • 01:37:48 should be simply an empty list so with
  • 01:37:50 these square brackets right here and
  • 01:37:51 well this empty list won't help us a lot
  • 01:37:54 but if you now scroll down again right
  • 01:37:56 here
  • 01:37:59 to our well game ending condition as I
  • 01:38:02 was referring to him then actually this
  • 01:38:05 round result variable is everything we
  • 01:38:07 need right here we have this round
  • 01:38:09 result variable which contains the
  • 01:38:11 dictionary I was referring to so if we
  • 01:38:13 could just add this dictionary to our
  • 01:38:16 list we are done already and this can be
  • 01:38:19 done with a default method available for
  • 01:38:21 lists so let's get rid of our print
  • 01:38:23 statement right here and let's now refer
  • 01:38:25 to our list so we call it game results
  • 01:38:27 and now we use a special method by
  • 01:38:30 adding a dots this shows us all the
  • 01:38:32 methods or functions available for lists
  • 01:38:35 and the method I'm referring to right
  • 01:38:37 here is the one on top
  • 01:38:38 it is append because it simply allows us
  • 01:38:41 to append an item to an existing list
  • 01:38:43 and what do we want to append right here
  • 01:38:46 well we want to append our round results
  • 01:38:49 of course like that not round results
  • 01:38:52 the round result sorry for that like
  • 01:38:54 this and we can of course also use this
  • 01:38:57 right here for our monster one case like
  • 01:38:59 that so with that we should be able to
  • 01:39:03 store the result of a round in our list
  • 01:39:06 then to start a new round with a
  • 01:39:08 different player name for example and as
  • 01:39:11 soon as this round ends we should also
  • 01:39:13 be able to add this result now so this
  • 01:39:15 new dictionary as the next list item to
  • 01:39:18 our list just to be sure we understand
  • 01:39:20 the code so let's have a look we start
  • 01:39:23 with this empty list right here as soon
  • 01:39:25 as we run our app – file then we start
  • 01:39:28 our game with our while loop right here
  • 01:39:29 and then we start our round
  • 01:39:33 we go through all our rounds so just as
  • 01:39:37 we did it before we know the code and as
  • 01:39:39 soon as either the player or the monster
  • 01:39:41 win the game we append the dictionary
  • 01:39:44 with the player results so in this case
  • 01:39:46 this dictionary or the dictionary down
  • 01:39:48 here to this existing empty list
  • 01:39:50 if this round was ended then we go back
  • 01:39:53 to our game while loop so this one right
  • 01:39:56 here but we don't declare this variable
  • 01:39:59 once again because we declared the game
  • 01:40:02 results right here outside of our while
  • 01:40:03 loops this means we use our current
  • 01:40:06 lists of the game results list
  • 01:40:08 containing one item for the next round
  • 01:40:10 then the code starts again as soon as
  • 01:40:12 the round ends we add the new result to
  • 01:40:14 this list and so on what will not work
  • 01:40:17 here is if we really end the game so if
  • 01:40:20 we use our exit game option right here
  • 01:40:21 then our list will not be stored so if
  • 01:40:24 you run the game once again then the
  • 01:40:26 result will be gone we could also change
  • 01:40:28 that by saving the result to a file but
  • 01:40:32 this is something we won't have a look
  • 01:40:33 at in this tutorial so let's see if this
  • 01:40:36 logic I was referring to is working now
  • 01:40:38 before we do that though we have to make
  • 01:40:40 sure we arm print our result also so
  • 01:40:44 let's print our game results right here
  • 01:40:49 and right there otherwise well we can't
  • 01:40:52 see a lot listen like that
  • 01:40:54 so let's now run the game let's enter a
  • 01:40:57 name for the player let's attack he'll
  • 01:41:00 he'll attack attack he'll so I'll play
  • 01:41:02 around a bit
  • 01:41:03 that looks good maybe we win this time
  • 01:41:05 yeah we can see that menu on the game we
  • 01:41:08 can see the health and the rounds let's
  • 01:41:10 now add another player maybe Tony and
  • 01:41:13 let's attack attack attack attack attack
  • 01:41:15 yeah and now we can see that we added
  • 01:41:18 the second dictionary also to our list
  • 01:41:21 so this is the second stat and we can
  • 01:41:23 also do this with another player let's
  • 01:41:26 say Sam quickly played a game and we can
  • 01:41:29 see Sam is also added right here to our
  • 01:41:32 list as you can see we have free list
  • 01:41:34 items and each list item right here is a
  • 01:41:38 dictionary so dictionary one or item one
  • 01:41:40 with the first dictionary item two with
  • 01:41:42 the second one and item free with the
  • 01:41:44 third dictionary and this is working
  • 01:41:47 nice
  • 01:41:47 but I'm not happy with two things now
  • 01:41:50 the first thing is that the result is
  • 01:41:53 printed automatically right here I think
  • 01:41:56 it would be better if the results or the
  • 01:41:58 printing of the results would be another
  • 01:42:01 option so another player choice so we
  • 01:42:03 could have a fourth player choice
  • 01:42:05 something like right here print for our
  • 01:42:10 show results something like that and in
  • 01:42:17 addition to that I don't want to show
  • 01:42:19 the results like this so inside this
  • 01:42:21 list and also one after another I would
  • 01:42:24 like to have them below each other so we
  • 01:42:26 have the first dictionary right here
  • 01:42:27 then in the second line we have the
  • 01:42:29 second pictionary and so on so let's
  • 01:42:33 first implement this fourth choice now
  • 01:42:35 and lets them work on the way the
  • 01:42:37 information is displayed now as we know
  • 01:42:39 the logic already we know what we have
  • 01:42:41 to do right here we have to create
  • 01:42:43 another Elif statement right here there
  • 01:42:46 the player choice is for well what
  • 01:42:49 should happen then well basically we
  • 01:42:52 only want to print our game results so
  • 01:42:54 we can delete this print statement right
  • 01:42:57 here from our player one and maybe cut
  • 01:43:00 this one from our monster and now simply
  • 01:43:02 add this right here as the fourth choice
  • 01:43:04 like that
  • 01:43:05 that's it already so let's exit the game
  • 01:43:08 and it started its player name if you
  • 01:43:13 know it for we should see nothing let's
  • 01:43:16 grab a bit yeah
  • 01:43:17 here is our empty list so this is
  • 01:43:18 working fine let's now attack and see
  • 01:43:21 what is happening then yeah now we see
  • 01:43:24 max won the game now we have to enter
  • 01:43:26 the player name for the next round so
  • 01:43:27 let's say Tony wants to start and Tony
  • 01:43:30 wants to see what the results from the
  • 01:43:32 previous players were so we can hit for
  • 01:43:34 now two shorter results and if we scroll
  • 01:43:37 up yeah we can see that the result of
  • 01:43:39 the last round is printed right here so
  • 01:43:41 this is working fine this was the first
  • 01:43:43 step the second step now is to well
  • 01:43:46 change the way the results are shown as
  • 01:43:48 I said I would like to show the results
  • 01:43:50 not inside the list but dictionary by
  • 01:43:52 dictionary line-by-line for that we need
  • 01:43:56 to have a look at the third control
  • 01:43:57 structure that is really important in –
  • 01:44:00 we had a look at
  • 01:44:02 if statements already we had a look at
  • 01:44:04 while loops which basically could
  • 01:44:05 basically control our entire application
  • 01:44:07 and now we have a look at for loops a
  • 01:44:11 for loop basically allows us to iterate
  • 01:44:14 well over a sequence a sequence is
  • 01:44:17 something like well a list or a
  • 01:44:19 dictionary in our case and to give back
  • 01:44:22 a result for each of these iterations
  • 01:44:24 now this sounds totally abstract of
  • 01:44:26 course therefore I would like to
  • 01:44:28 implement the for loop now step by step
  • 01:44:30 then see how it works and then also
  • 01:44:33 compare it to the while loop we created
  • 01:44:34 so far now where do we create such a for
  • 01:44:37 loop well right here in our fourth
  • 01:44:39 choice because what do I want to achieve
  • 01:44:41 right here as I said I would like to
  • 01:44:44 have the look at the content of our
  • 01:44:46 lists of our game results list right
  • 01:44:49 here and then I would like to basically
  • 01:44:52 print or display each single item off
  • 01:44:55 this list separately so if we have three
  • 01:44:58 items inside our list I don't want to
  • 01:45:00 print the entire list
  • 01:45:02 I would like to go for the first item
  • 01:45:04 print that I would like to go to the
  • 01:45:06 second item print it and then I would
  • 01:45:08 like to go to the third item print it
  • 01:45:10 and so on this is what I meant with
  • 01:45:12 iterating over a sequence over a list in
  • 01:45:15 that case so basically what I want to
  • 01:45:17 tell Python is have a look at this list
  • 01:45:19 iterate through each item this list
  • 01:45:22 contains and printed separately that's
  • 01:45:25 what I would like to achieve right here
  • 01:45:26 and for that we can use the for loop I
  • 01:45:29 was referring to you the for loop is
  • 01:45:31 written like this we add four and now we
  • 01:45:34 can add a name of your choice because
  • 01:45:36 what you're now saying Python is
  • 01:45:38 basically for each item inside a list do
  • 01:45:42 something and for each item you can
  • 01:45:45 specify a name of your choice so for
  • 01:45:47 example you could say for item in game
  • 01:45:53 results like that so you say for each
  • 01:45:56 item you find in the game results list
  • 01:45:58 please do something at the moment we
  • 01:46:01 will just delete that and say we don't
  • 01:46:02 do anything we just pass right here
  • 01:46:06 important item doesn't have to be named
  • 01:46:09 item right here you could also name this
  • 01:46:10 for score in game results
  • 01:46:15 or player stat in game results whatever
  • 01:46:20 this is simply what you name each single
  • 01:46:23 list item in your list so your can name
  • 01:46:25 this as you prefer the important thing
  • 01:46:27 here is that you say that for each item
  • 01:46:30 in this is also important you have to
  • 01:46:32 put this right here in the list in our
  • 01:46:34 case so in this game results variable
  • 01:46:36 please do something what you could do
  • 01:46:39 now is for example you can simply say
  • 01:46:41 that for each item you find inside our
  • 01:46:44 list please print and now important this
  • 01:46:47 item here the naming is important you
  • 01:46:49 have to name this right here equal to
  • 01:46:53 the name you gave to each item inside
  • 01:46:55 your list just repeat that what you're
  • 01:46:57 not doing right here is we say please go
  • 01:46:59 for the list so the list saved in our
  • 01:47:02 game results variable right here and for
  • 01:47:04 each item which we named players that
  • 01:47:07 right here in this for loop please print
  • 01:47:09 me the corresponding item now let's see
  • 01:47:11 if this works now so we can clear this
  • 01:47:14 now we can run the code once again and a
  • 01:47:17 name menu attack attack attack attack
  • 01:47:20 attack as always then we add Tony as the
  • 01:47:23 second player we also attack all the
  • 01:47:25 time
  • 01:47:25 maybe he'll it once or twice like that
  • 01:47:27 and now we enter a third player maybe
  • 01:47:31 Sam once again and now we show the
  • 01:47:33 results if we scroll up a bit you can
  • 01:47:36 see that now we have two changes
  • 01:47:39 compared to the previous print statement
  • 01:47:41 we had without the for loop the first
  • 01:47:43 thing is that the items are no longer
  • 01:47:46 stored in a list before we had the
  • 01:47:48 square brackets around the dictionaries
  • 01:47:50 because well the dictionaries purchased
  • 01:47:52 single list items additionally we also
  • 01:47:55 print the items line by line now because
  • 01:47:58 these are now single dictionaries
  • 01:48:00 because we simply access the
  • 01:48:02 dictionaries inside our lists what
  • 01:48:04 basically took them out of the list
  • 01:48:06 right here and printed them line by line
  • 01:48:08 so players dead right here so for
  • 01:48:11 players that is simply this dictionary
  • 01:48:13 right here and that dictionary right
  • 01:48:15 there so that's the for loop and what's
  • 01:48:19 the difference now to the while loop
  • 01:48:20 that we previously made well here we
  • 01:48:22 iterate over the items inside our list
  • 01:48:25 as soon as the race is over
  • 01:48:26 well the for loop
  • 01:48:28 ends our Y loop simply keeps running as
  • 01:48:31 long as a certain condition is true so
  • 01:48:34 if we never change the new round to
  • 01:48:36 false the new round will continue
  • 01:48:38 infinitely so we will never finish or
  • 01:48:41 exit this while loop this continues
  • 01:48:43 forever basically until the condition
  • 01:48:45 you define right here ends for the for
  • 01:48:47 loop this is different because the for
  • 01:48:49 loop will simply end as soon as the
  • 01:48:51 iteration already lists items in our
  • 01:48:53 case ends so as soon as the for loop
  • 01:48:55 looped through all the items well the
  • 01:48:57 for loop automatically ends in our case
  • 01:48:59 so that's basically difference if you
  • 01:49:01 want to iterate few items use a for loop
  • 01:49:03 if you want to keep executing a code
  • 01:49:06 until a certain condition is no longer
  • 01:49:07 true then use a while loop so with that
  • 01:49:11 we are actually done with our game right
  • 01:49:12 here but now it's the time to have a
  • 01:49:15 look at our code once again and see if
  • 01:49:17 we have some issues or if we can improve
  • 01:49:19 some things here specifically I want to
  • 01:49:21 work on one little problem you could say
  • 01:49:24 that we have right here I showed you
  • 01:49:26 that earlier in the video when I called
  • 01:49:29 the calculate monster attack function
  • 01:49:31 right here before we actually declare
  • 01:49:33 the monster variable if you remember
  • 01:49:36 back what we learned about parameters
  • 01:49:37 and arguments then this is exactly what
  • 01:49:39 can be helpful here not what they mean
  • 01:49:41 by that why don't we just say that we
  • 01:49:43 add two parameters right here to our
  • 01:49:46 calculate monster attack function for
  • 01:49:48 example attack min as the first
  • 01:49:50 parameter and attack max as the second
  • 01:49:53 parameter now we can copy them and paste
  • 01:49:55 them right here to our rent in function
  • 01:49:58 to be able to use them and now we only
  • 01:50:00 have to go down to our calculate monster
  • 01:50:05 attack function so these two functions
  • 01:50:07 right here and we can see the error
  • 01:50:09 already we didn't add any arguments to
  • 01:50:12 the function we can change that quickly
  • 01:50:14 because the first argument should be the
  • 01:50:16 monster attack min right so referring to
  • 01:50:20 our monster variable and the second one
  • 01:50:24 would be monster attack next like that
  • 01:50:30 let's quickly
  • 01:50:33 copy these down here to the second
  • 01:50:36 monster attack so this is working now so
  • 01:50:39 we shouldn't have an issue but now if I
  • 01:50:43 again want to print our function before
  • 01:50:48 we declare the monster variable so
  • 01:50:51 calculate monster attack right here then
  • 01:50:56 we can add monster attack min and
  • 01:51:01 monster attack next once again so if we
  • 01:51:09 save that we get an error now right here
  • 01:51:12 because as you can see we are using our
  • 01:51:15 variable monster before we are actually
  • 01:51:17 assigning it this means if I cut the
  • 01:51:20 print statement now and put it below our
  • 01:51:23 variable we can see this error is gone
  • 01:51:26 and with that we improved our code a lot
  • 01:51:28 because now we can immediately see that
  • 01:51:31 if we want to call the function to
  • 01:51:33 earlier well that we get this error and
  • 01:51:35 that we first have to declare the
  • 01:51:36 variable so with that this approach
  • 01:51:38 right here is the one to prefer because
  • 01:51:41 it makes sure that our code runs
  • 01:51:42 correctly and it helps us avoiding
  • 01:51:44 errors like that let's delete the print
  • 01:51:46 statement though and let's also get rid
  • 01:51:49 of that empty line and let's now finally
  • 01:51:52 see if it's the wroking so we run the
  • 01:51:53 game and for a name tag he'll maybe
  • 01:51:58 crease it up a bit right here attack yep
  • 01:52:01 this is working let's say Tony wants to
  • 01:52:03 play again play yeah this works also
  • 01:52:08 let's not say Sam wants to see the
  • 01:52:11 results
  • 01:52:14 yep this works fine and now we want to
  • 01:52:16 exit the game so with that we refactor
  • 01:52:19 our app and also finished our app for
  • 01:52:22 this video I hope you got a good
  • 01:52:25 understanding now of the core basics of
  • 01:52:28 Python and especially how you can apply
  • 01:52:30 them in practice because learning to
  • 01:52:32 code in theory is nice but I think
  • 01:52:35 applying the knowledge and learning it
  • 01:52:37 by really practicing it in a practical
  • 01:52:39 project like this is a lot more helpful
  • 01:52:41 at least in my opinion so with that I
  • 01:52:43 wish you all the best for your Pyfrom
  • 01:52:45 projects dive deeper into Python it's an
  • 01:52:48 awesome language it's a lot of fun to
  • 01:52:50 work with and also make sure to have a
  • 01:52:52 look at academy.com
  • 01:52:53 there you can also find more Python
  • 01:52:55 content and also a lot more content
  • 01:52:58 mainly related to programming and web
  • 01:53:00 development and with that well thanks a
  • 01:53:02 lot for watching and bye