Python Forum
Python Hangman Game - Multiple Letters Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Hangman Game - Multiple Letters Problem
#1
Python 3.6.5

I've created a hangman game in Python and I'm trying to debug an error:

The game works in a way that randomly picks a word from a list, stars out the word (using two separate variable names), and then each time a letter is guessed correctly, the star is replaced with the correct letter in the correct position. However, for instance the word "seen". If you correctly guess the letter "e" it will populate the guess as *e**. It will not add two e's. This is because I'm using the .find() function, which stops after finding the correct letter:
if guess in full_word: #replacing the * in selected_word with the guessed letter that was correct
             checker = full_word.find(guess)
             selected_word = selected_word[:checker] + guess + selected_word[checker+1:]
can anyone offer any advice that allows the program to check multiples of the same letter?

thank you!
Reply
#2
The find function returns the index of the first instance found. That's why it stops working after the first one is found. If it doesn't find an instance then it returns -1. What you need to do is keep running find in a loop until it returns -1.
Something like:
while full_word.find(guess):
    selected_word = selected_word[:checker] + guess + selected_word[checker+1:]
Reply
#3
(Jun-04-2020, 02:26 PM)Clunk_Head Wrote: The find function returns the index of the first instance found. That's why it stops working after the first one is found. If it doesn't find an instance then it returns -1. What you need to do is keep running find in a loop until it returns -1.
Something like:
while full_word.find(guess):
    selected_word = selected_word[:checker] + guess + selected_word[checker+1:]

I'm not getting this to work. Wouldn't it still just have the same problem? full_word is the original word, and selected_word is the starred out version. If I just keep running .find on full_word with guess, surely it would still keep ending at the first index?
Reply
#4
I instead of using find, why not compare each letter? I did that here:

https://python-forum.io/Thread-else-cond...#pid111183
Reply
#5
Thank you! This worked. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Stacking Problem in a game. HelloTobi22 2 960 Aug-05-2022, 09:48 AM
Last Post: HelloTobi22
  Problem with my pong game code Than999 8 3,760 May-15-2022, 06:40 AM
Last Post: deanhystad
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,494 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  multiple condition if statement problem FelixReiter 3 2,541 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  eliminating letters when typed in python window deebo 0 1,706 Jan-05-2021, 09:26 AM
Last Post: deebo
  Guessing game problem IcodeUser8 7 3,518 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  game of the goose - dice problem koop 4 3,410 Apr-11-2020, 02:48 PM
Last Post: ibreeden
  Problem with accepting multiple string inputs Ryan_Todd 5 2,878 Jan-22-2020, 06:12 PM
Last Post: buran
  Plot multiple csv into one graph problem with visualize linkxxx86 1 5,668 Oct-14-2019, 05:54 PM
Last Post: linkxxx86

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020