Cara menggunakan word guessing game python

By On Thursday, October 13th, 2022 Categories : Tanya Jawab

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
follows:

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:

The screenshot
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).

Introduction:

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 ‘_’.
e.g.
DONALD TRUMPS
becomes
_ _ _ _ _ _ / _ _ _ _ _ _
3. Finally, the
player must guess the word by suggesting different letters.

Rules:

  • 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. 

STEP 1
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”
position = 6
character = “e”

string = string[:position] + character + string[position+1:]
print(string)

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 ‘_’.

Challenge 1
Modify the code above so that it changes the 3rd letter to an underscore ‘_’.

STEP 2
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”
character = “_”
for i in range (len(string)):
  string = string[:i] + character + string[i+1:]
print(string)

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.

STEP 1
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:

import random

names = [“Bob”, “Dave”, “Stuart”]

print(names[random.randint(0,2)])

Challenge:

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:

print(names[0])

The
computer would return the name “Bob”

STEP 2
Replace the last line with the following new code:

print(random.choice(names))

Run the new script and explain what the new code does.

 

STEP 3 

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.

Challenge:

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.

​import random

#create a list of hangman words
wordList = [“cat”,”dog”,”mouse”]

#choose a word from the list at random
wordChosen = random.choice(wordList)

#create an empty list to store the used letters
used = []

#create a variable to store and display the player’s guesses  
display = wordChosen
for i in range (len(display)):
  #replace each letter with a ‘_’
  display = display[0:i] + “_” + display[i+1:]

  #put
a space between each dash
print (” “.join(display))

#counter stops the game once all letters have been guessed correctly
attempts = 0

#keep asking the player untill all letters are guessed
while display != wordChosen:
  guess = input(“Please enter a letter: “)
  guess = guess.lower()
  #Add the players guess to the list of used letters
  used.extend(guess)
  print (“Attempts: “)
  print (attempts)

   
#Search through the letters in answer
  for i in range(len(wordChosen)):
    if wordChosen[i] == guess:
      display = display[0:i] + guess + display[i+1:]

  print(“Used letters: “)
  print(used)

        #Print the string with guessed letters (with spaces in between))
  print(” “.join(display))
  attempts = attempts + 1

  print(“Well done, you guessed right!)

Example Solutions


Challenge:

  1. Modify you code so that it tells the player how many guesses they made.
  2. Add some more words or phrases to your list.
  3. If your phrase contains a space, replace the space ” ” with a slash “https://kafesentul.com/”.
  4. 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). 

Licence:

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.

Cara menggunakan word guessing game python | admin | 4.5
shopee flash sale gratis ongkir
x