Skip to main content

Posts

The Driver Function

Now, after looking into important function let's take the number of inputs from the user for incorrect attempts and lets get started def get_num_attempts (): while True : num_attempts = input( 'How many incorrect attempts do you want? [1-5] ' ) try : num_attempts = int(num_attempts) if 1 <= num_attempts <= 5 : return num_attempts else : print( '{0} is not between 1 and 5' .format(num_attempts)) except ValueError: print( '{0} is not an integer between 1 and 5' .format(num_attempts)) As we can see we'll give user max number of 5 attempts and min. number of 1 attempt After looking into all the function and learning every bit of information needed to create this game now its time to look into the main function which calls all the other function when required and does the game play smooth and systematic. we'll look into code in p...

User Friendly Function

So, here comes the last one and the user's function, here, in this function we will look into how user will be given hints to solve the puzzle and how the solving will go and how he/she won't be able to cheat!  Let's jump directly into the code and see how that will happen: CODE:       def   get_next_letter ( remaining_letters ):      """Get the user-inputted next letters."""      if   len (remaining_letters) ==  0 :          raise   ValueError ( 'There are no remaining letters' )      while   True :         next_letter =  input ( 'Choose the next letter: ' ).lower()          if   len (next_letter) !=  1 :        ...

Getting the Display Word!

After, getting the word length from the user,its time for getting the word of exact same length as given by the user from the database! Its time to look into our third function that is the get display word function. Without further wasting time let's get into the code.. CODE:    def   get_display_word ( word ,  idxs ):      """Get the word suitable for display."""      if   len (word) !=  len (idxs):          raise   ValueError ( 'Word length and indices length are not the same' )     displayed_word =  '' .join(         [letter  if  idxs[i]  else   '*'   for  i, letter  in   enumerate (word)])      return  displayed_word.strip() As we can, here w...

Getting the Word Length

Today, let's look into another function which constitutes the working of the game.       So, the second function we are going to see is to get the minimum word length from the user. Here, user will enter his word preferred word length to increase his chances of winning and saving himself. Lets get to the code part! CODE:      def   get_min_word_length ():      """Get user-inputted minimum word length for the game."""      while   True :         min_word_length =  input (              'Choose minimum word length [2-16] ' )          try :             min_word_length =  int (min_word_length)     ...

How does the game work?

In today's post we're going to have a look on the functions which constitute the working of this game. So,the first function we are writing is to "get the no.of attempts"              Here, the user is getting the choice to have as much as  5 chances to solve the puzzle. Interesting,right? Let's get into the code part! CODE: def  get_num_attempts():      """Get user-inputted number of incorrect attempts for the game."""      while   True :         num_attempts = input( 'How many incorrect attempts do you want? [1-5] ' )          try :             num_attempts = int(num_attempts)       ...

The Hangman Graphics Function

So now comes the most interesting part..... Every game is incomplete without some graphics ,so we introduced a function hangman_graphic that is called whenever the attempts remaining are decremented and displays stages of hangman and when the attempts are over then a complete graphic of hangman is displayed! Following is the function code:- def hangman_graphic (attempts_remaining): if attempts_remaining == 5 : print ( "________ " ) print ( "| | " ) print ( "| " ) print ( "| " ) print ( "| " ) print ( "| " ) elif attempts_remaining == 4 : print ( "________ " ) print ( "| | " ) print ( "| 0 " ) print ( "| " ) print ( "| " ) print ( "| ...

Learning the Enumerate()

After, the list lets learn about the intriguing enumerate function in python. Python is the favorite programming language of many developers; contrary to popular belief, it isn’t merely a “technology for newbies” its power and functionality make it an excellent tool for a plethora of tasks. Python is renowned for its collection of libraries, modules, and functions that it comes packaged with and enumerate() is something we should learn in detail.           So, what is enumerate function?           Python's enumerate function is tool that allows us to create a counter alongside values we're iterating over: as part of a loop, for example. Enumerate takes an iterable type as its first positional argument, such as a list, and returns an enumerate object containing a number of tuples: one for each item in the iterable. Each tuple contains an integer counter, and a value from the iterable. If we ever have to iterate through...