Cara menggunakan word guessing game python
Cara menggunakan word guessing game python – Halo sobat semua, Terima Kasih sudah berkunjung ke halaman blog trendwisata.com ini. Pagi ini, kita dari portal www.trendwisata.com pengen sharing Q&A yang oke banget yang membahas Cara menggunakan word guessing game python. Kami persilakan sobat melihat dibawah ini:
In my last post, I built a numeric guessing game in Python and the link for this post can be found here: Build a computer guessing game in Python
In this post I have made a guessing game, where the computer selects a word at random from a list of words, and the player has to guess the correct word by guessing
which letters are actually in the word. The words that the computer selects at random are taken from the SOWPODS dictionary, which was compiled by Peter Norvig, and the link to the file can be found here: norvig.com/ngrams/sowpods.txt
The program is written around a series of while loops, for statements, and conditional statements that allow the player to keep guessing the letters
contained in the correct word until he finally is able to guess the word. The rules of this problem can be found in this link: 31 Guess Letters.
The diagram below is a flowchart that I created to explain the mechanics of the game. I have written this flowchart on MS Word on my laptop computer and have pasted it to this post. The flowchart reads as
At the beginning of the program, the libraries are imported, the SOWPODS text file is loaded into the program and converted to a list, and the variables are defined and initialised. The program selects a word at random, rand_word, from the list and another list, play_word, that will store the letters is created as well.
The two lists are input into a while loop, which will run until the two lists match each other. The player will be asked
to guess the letter and the variable, guess_no, will be incremented.
If rand_word is not the same as play_word, the guessed letter will be put into a for loop, which will check to see if the guessed letter, play_letter, matches any of the letters in rand_word.
The two letters, play_letter and the letters in rand_word, will be put into an if statement. If the two letters match then play_letter will be inserted into play_word at the correct placement.
Once the player has correctly guessed all of the letters contained in rand_word, the while statement stops running, the player is congratulated, and a message is printed to say how many guesses it took to guess the word.
The screenshot below is the code for the beginning of the program where the libraries are imported, the SOWPODS file is read and converted to a list, and the variables are defined and then initialised:
below is the code of the while statement that tests to see if play_word matches rand_word and the if statement, and for loop. Else statements are also included in the while statement and if statement if the inputs do not meet the criteria the program is asking for:
Below is the output of the game, which gives the player messages to help him to correctly guess the word. Once the player guesses the word, he is given a congratulatory message and told how many times
he guessed before he got it right:
In summary, this was somewhat of a complex program to write because I had to convert the dataframe to a list before it would work. Python uses different functions for lists than it does for dataframes, so I had to utilise different techniques to be able to successfully write the program.
Guess the word (Python tutorial)
How to create a ‘guess the word’ game in Python (Free python web tutorial).
How the game works
1. The game will start by picking a random word or phrase from a list.
2. Next, each letter in the word or phrase is replaced by a dash ‘-‘ or ‘underscore ‘_’.
_ _ _ _ _ _ / _ _ _ _ _ _
3. Finally, the
player must guess the word by suggesting different letters.
- If the player guesses a correct letter, the letter is revealed in the phrase.
- For each letter the player guesses wrong, the player will lose a life.
In this tutorial, you will learn how to create your very own ‘guess
the word’ game using Python.
Part 1: Introducing the String Slicing
Before creating your guess the word game, let’s explore some of the functions you will be using.
Let’s start by writing a program to replace a character at specific index in a string.
To replace a character with a given character at a specified index, you can use python string
slicing as shown below:
Type in the following code in the code window (below):
string = “pythonhxamples”
string = string[:position] + character + string[position+1:]
Make sure you comment your code, using the hashtag (#), explaining what the code is doing.
In our game, we need to replace each letter in our word or phrase into a dash ‘-‘ or ‘underscore ‘_’.
Modify the code above so that it changes the 3rd letter to an underscore ‘_’.
In order to change all the letters in our word or phrase, we are going
to need a loop.
Type in the following code in the code window (above):
string = “TRUMP”
Problem! Can you see the problem with our code?
Because there are no spaces in between each of the hidden letters, we can’t tell how many letters there are in our word!
Replace the last line in your code (above) with the following (make sure you put a space between the two speech marks ” “):
Run your code again and comment your code, using the hashtag (#),
explaining what the new code is doing.
Part 2: Creating a random name picker
To make our game more challenging, each time the player plays the game it will pick a new word at random from a list. Let’s practice this by creating a random name picker.
Let’s create a random name picker. This will form the basis of your ‘guess the word’ game.
Type in and run the following code:
names = [“Bob”, “Dave”, “Stuart”]
Comment your code, using the hashtag (
#), explaining what the code is doing and why the random generator is returning a value between 0 and 2 rather than 1 and 3.
Did you know?
Lists start at 0 NOT 1.
e.g. if we were to run the following code:
computer would return the name “Bob”
Replace the last line with the following new code:
Run the new script and explain what the new code does.
Finally, we need to add (concatenate) the word “Minion” at the beginning of our randomly selected name.
Replace the last line with the following:
print(“Minion” + “ “ + random.choice(names))
Run the new script and explain what the new code does.
Add some more names to the list.
Part 3: Creating the ‘guess the word’ game.
You now have all the components you need to create your very own guess the word game. Enter the following sample code into the code window below.
#create a list of hangman words
#choose a word from the list at random
#create an empty list to store the used letters
#create a variable to store and display the player’s guesses
#counter stops the game once all letters have been guessed correctly
#keep asking the player untill all letters are guessed
print(“Used letters: “)
#Print the string with guessed letters (with spaces in between))
print(“Well done, you guessed right!)
- Modify you code so that it tells the player how many guesses they made.
- Add some more words or phrases to your list.
- If your phrase contains a space, replace the space ” ” with a slash “https://kafesentul.com/”.
- Add a ‘life counter’ and remove a life every time a player guesses an incorrect letter. The game ends if the player guesses correctly or they run out of lives (which ever comes first).
Unless otherwise specified, everything in this repository is covered by the following licence:
Tags: programming language python, python programming language, python coding language, programming languages python, python code language, python programming language
example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding, best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in
computers, how does computer code work, what is computer programmer.