![]() If the player chooses any other letter apart from ’ S’ / ‘ F’, the player will be prompted to select a valid category (’ S’ or ‘ F’) or ‘ X’ to exit. NOTE: In case you have more word categories, you can add corresponding elif blocks similar to this. In this case, a word from the ‘ fruits ’ list will be picked randomly using the ‘ random.choice‘ from the ‘ random‘ module and stored in the ‘ secretWord ’ variable. The elif (else if) block of the code will be executed if the player chooses ’ F’ indicating that they want to play the game with words from the ‘ fruit’ category. The word will then be stored in the ‘ secretWord ’ variable. In this case, a word from the ‘ superHeroes ’ list will be picked randomly using the ‘ random.choice‘ from ‘ random‘ module. The if block of the code will be executed if the player chooses ’ S’ indicating that they want to play the game with words from the ‘ superhero’ category. An option to exit the game is also provided, in case they decide not to play. The players are given an option to choose a category (Fruits / Superheroes) for the word they would like to guess. Here we will make use of the while loop and if/elif/else conditionals to pick the random word. while True:Ĭategory = input("Please select a valid category: F for Fruits / S for Super-Heroes X to exit") Next, we will add the logic to allow the program to choose a random word from the desired category. ![]() Time.sleep(1) STEP 4: Choosing a random word from the desired category Don't forget to press 'enter key' after each guess.") Print("You can guess only one letter at a time. Print("The objective of the game is to guess the secret word chosen by the computer.") Print("Hello", name.capitalize(), "let's start playing Hangman!") Here we are using the time module’s ‘ sleep()‘ method to pause for a few seconds between the displays. Before starting the game, we display some details about the game. ![]() We will prompt the player for their name and store it in a variable. We will create a few variables to store the game statistics: userGuesslist = You can make one or more word lists depending on the number of categories you want to add in the game. The two lists were created to give the players the option of picking the category for the word they would like to guess. I created two lists in this program: the first one has fruit names and the second one consists of superhero names. Next, we will create our word list(s) that will be used by the program to pick words randomly for the players to guess. import random, time STEP 2: Create word-lists and variables The ‘ choice()‘ method from the ‘ random’ module will be used to pick random words from our word lists and the ‘ sleep()‘ method from the time module will be used to introduce delays where needed. Step 1: Importing Modulesįirst, we will import ‘ random‘ and ‘ time’ modules. We will also make use of the random and time module. The Python concepts that we will use in this game are: strings, loops, conditional (If/else) statements, lists, and functions. In this tutorial, we will look at one of the ways to make the classic word guessing game ‘Hangman’.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |