Project Based Python: Guess The Phrase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to project-based python these tutorials will not cover python basics in depth if you need help with the basics you can try pi learning companion it is a python study app that covers all the core concepts in short lessons then test your skills with hundreds of activities pi learning companion is available on android and ios you can find the links in the video description today we will create a phrase guessing game similar to hangman we will use dictionaries loops and if statements the program will present the user with an encoded phrase which they have to decode by guessing letters let's get started we will start by creating a secret phrase and we're going to save that secret phrase in the variable called answer and for the secret phrase we will say this is the secret phrase now we're going to create another variable which is going to be my answer and that variable is going to hold stars instead of letters that way we can show the user how much progress they have made by guessing letters it will look something like this so we'll have this is the and so on so we will make sure that there are spaces to allow the user to see how many words there are to accomplish this we are going to use a dictionary and another string so first my answer dictionary will equal an empty dictionary and then my answer will equal an empty string we are going to create a for loop now to iterate through the answer and then create entries in the dictionary that we will then use to create my answer so this will make more sense in a minute for i in range blend answer now this is going through the answer and i is going to take on the index value of each character in the answer we are then going to use that i as the key in our dictionary so if answer i first we're going to look for spaces if answer i equals a space we will update the dictionary so my answer dictionary equal not equals update and we will use i as the key and a space as the value so we're updating the dictionary with i and space we will also update my answer so my answer plus equals and just a space if the character that we're looking at is not a space if it is a letter we're going to enter a star as the value so else my answer dictionary update and i as the key and then star as the value and also my answer plus equals stock so let's see what that looks like we will just print answer then we will print my answer and we will print the dictionary just so we can see that too so here we go we have this is the secret phrase we have stars that match each individual letter we have spaces where they should be and then down here we have the full dictionary which has key values of index value 0 for t [Music] one for h and so on there are stars at the moment but we can use these key values to later update the stars to actually display the letters now let's get rid of these print statements because we don't need them any longer and we will create a few more variables that we will use so first guest letters will equal an empty string for now we will use this variable to hold all of the different letters that the user has guessed then we need count and we'll set that to equal zero we will use the count to keep track of how many guesses the user has made and now we need multiple letters and this will start as false and we will use this boolean to check that the user is only entering one letter at a time we don't want them guessing five letters at a time because that would be cheating so next let's have a print statement that kind of tells the user what to do we can say print can you the cipher the text and then we will print the encoded text so my answer which will be made up of only stars and if i run this we get can you decipher the text and we get all of these stars now let's create the while loop and start getting some user input so while count is smaller than 10 so we are doing this to make sure that the user doesn't keep guessing uh we will first reset our answer so my answer means to equal an empty string here it starts off with all of those stars and we will reset it every time we start we iterate through the while loop and then we will populate it later on so now we need to check if the user has entered multiple letters we don't want that to be the case so if not multiple letters we can proceed and we will try to get the full answer here so we will say guess answer equals input guess the full text so we're giving the user a chance to guess the full phrase before they start guessing letters and now we need to check if that guess is correct so if guess answer equals answer we will print you win and we will also break out of the loop all right and let's add one more line here we will just say else print incorrect guess and let's test this out so we have can you decipher the text we have all those stars and let's say no so incorrect guess and we try again so let me guess correctly here so this is the secret phrase and there it is you win next we are going to get the user to guess individual letters so first we're going to set multiple letters to equal false because we no longer need that and we want it to reset after we perform this check now we will get a guess and that will be another input and we will say guess a letter once we have that letter from the user we will check how long their guess is so this is where the multiple letters boolean comes into play we're going to check if the guess is only one character long if it is we will proceed otherwise we will deal with the multiple letters later so first we will check the length so if len guess is one we are going to create a loop now to populate the dictionary and then the answer so 4i in range len answer we're doing the same thing as we did up top so for each character in answer if that character so answer i equals the guess that the user has guessed we need to update the dictionary so my answer dictionary dot update and we will use the i as the key and then we will use guess as the value so this will replace any star so if the guess equals a letter in the answer the dictionary will then replace the stars by those letters and once we have that done we need to create another for loop so for i in my dictionary or my answer dictionary my answer plus equals my answer dictionary and then i so here what we're doing is we are going through each entry in the dictionary we are adding each one of those values to our answer and let's print both of those to the screen just so we can see what we have here so we will print my answer and we will print my dictionary so let's run this we have can you decipher the text we have the encoded text let's say no for now and then we have guess a letter so i will try t and you can see we have all of the t's that are in the answer they are no longer stars they are t's and then in our dictionary we can see that in the answer index value 0 is now set to t it is not a star any longer let's remove these two print statements now because we don't really want them to show up in our game and we need to tab in to get inside of this if statement the next thing we're going to do is compare our answer to the correct answer we're doing that because technically the user could enter something wrong up here but have guessed all of the letters that are in the answer which would then display the full answer to them so they will win by default and this is simple we will just say if my answer equals answer we will print my answers so we can show them the false the full phrase we will print you win and we will also break out of the while loop and finish the game next we are going to handle the guest letters so we want to update the guest letters to show the user which letters they have guessed and the first thing we need to do is check the length of guest letters so if len guess letters equals zero we will do one thing and if it is larger than zero we will do another thing what i mean by that is we if there have been no other guesses before we just add the letter if there are other guesses present we want to add a comma a space and then the letter so here we will just say guess letters plus equals guess now like i said if the length is longer than zero we will update guest letters with plus equals we will use a comma and a space and then plus yes so this will make it look a little bit nicer the next thing we need to do is print my answer and then we will print all of the guest letters so print and we'll use a formatted string here so f and we'll just say guest letters and we need to pass that variable in here all right and let's test this out so we have can you decide for the text and we have it here in stars guess the full text i will say no and then guess a letter so we will try t we get all of the letters updated here we have the displayed guest letters so we just have a t here let's do one more round and let's say h and you notice that we update it and then we also update the guest letters and here we add a comma in the space let me have one more guess and i will guess a letter that doesn't that is not in the phrase and we can say we can see that the letter is not added here because it's not part of our string but the guest letters are updated so we have guess th q okay we're almost done so all that's left to do is we need to update our count so we will increase the count by one then we will display how many guesses remain and we will only do that if the count is smaller than ten so count or if count smaller than 10 print and we'll use a formatted string and we will just subtract the count from 10 so that this number will always change and we'll say guesses remaining and then we need to handle the multiple letters or multiple yeah the multiple letters so remember up here we are checking the length of our guess and if that length is one we will handle all of this but if that length is not one so if it is multiple letters and i guess zero we will create an else statement and we'll print please enter only one letter so that will cover that and we also need to set multiple letters to equal true we also need or we don't need it but we will add a print statement down here to indicate that the game is over so once we are done with everything we will display game over and let's just test this so we will get the can you decipher the text we have all the stars i will say no for now guess a letter and let me guess multiple letters here we get please enter only one letter and notice we don't update the guest letters we don't increase the count none of that stuff happens because the guess is not valid so now we will guess another letter so let's just say p and it is updated in our string we get the guest letters and we have nine guesses remaining so let's do a few more we'll just say no and i will guess e no and i will guess h so we can see that the text is updating we can see that the guest letters are updating and let's now guess the full phrase so this is the secret phrase and there it is you win game over so now that we have a fully functional game let me show you a few things that you can do as an extension to this first of all let's change this phrase let's just say hello there and test it out to show that everything works you can see that the number of stars has changed guess the full text i will say no and let's try l and everything works as it should so i'll say hello there and you win game over so that part works now one thing that you could do as an extension is you could create a list so we could do something like answers equals and have a list and then here we can have something and something else or another phrase and so on so you could create a list of phrases here and then randomly choose one of these phrases and set that phrase to equal the answer so this game can be made much more interesting that way but we are not going to cover that here we are just going to leave it as is if you have enjoyed this tutorial make sure to like and subscribe also check out pi learning companion the link is in the video description you can also find the source code below
Info
Channel: TMD Studios
Views: 271
Rating: undefined out of 5
Keywords: python, python3, coding, learn, programming, project, tutorial
Id: DU1qMhyKv8g
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Tue Nov 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.