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
  Problem restricting user input in my rock paper scissors game ashergreen 6 876 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  multiple condition if statement problem FelixReiter 3 566 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  eliminating letters when typed in python window deebo 0 374 Jan-05-2021, 09:26 AM
Last Post: deebo
  Guessing game problem IcodeUser8 7 913 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  game of the goose - dice problem koop 4 1,278 Apr-11-2020, 02:48 PM
Last Post: ibreeden
  Problem with accepting multiple string inputs Ryan_Todd 5 1,028 Jan-22-2020, 06:12 PM
Last Post: buran
  Plot multiple csv into one graph problem with visualize linkxxx86 1 3,122 Oct-14-2019, 05:54 PM
Last Post: linkxxx86
  Hangman metro17 4 1,102 Sep-18-2019, 10:59 AM
Last Post: perfringo
  re.split multiple delimiters problem gw1500se 2 1,876 Jun-24-2019, 02:43 PM
Last Post: gw1500se
  Hangman code problem KrakowKid 1 1,030 Feb-25-2019, 06:29 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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