I thought about having a word as a string, making it into a "regularList" of strings, generating a "dummyList" which contains a string '-'for each letter in the word , then looping through the "regularList", simultaneously removing each instance of my guessed letter from "regularList" and reassigning it to the same index of "dummyList". Basically, I need to make:
regularList = [['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k']]
dummyList = ['_','_','_','_','_','_','_']
Into:
regularList = [['r', 'd', 'v', 'r', 'k']]
dummyList = ['a','a','_','_','_','a','_','_']
Here is my attempt:
word = 'aardvark'
def changeLetter(word):
guess = raw_input('Guess a letter:') # When called, guess:a
print word
dummyList = []
for i in word:
dummyList.append('_ ')
print dummyList
regularList = [list(i) for i in word.split('\n')]
print regularList
numIters = 0
while guess in regularList[0]:
numIters += 1
index = regularList[0].index(guess)
dummyList[index] = guess
del regularList[0][index]
print regularList
print dummyList
print numIters
changeLetter(word)
This code produces:
Samuels-MacBook:python amonette$ python gametest.py
Guess a letter:a
aardvark
['_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ']
[['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k']]
[['r', 'd', 'v', 'r', 'k']]
['a', '_ ', '_ ', 'a', '_ ', '_ ', '_ ', '_ ']
3
As you can see, the proper indices aren't being reassigned.
word = 'aardvark'
def changeLetter(word):
guess = raw_input('Guess a letter:') # When called, guess:a
print word
dummyList = []
for i in word:
dummyList.append('_ ')
print dummyList
regularList = [list(i) for i in word.split('\n')]
print regularList
numIters = 0
position = 0
length = len(regularList[0])
while numIters < len(regularList[0]):
if regularList[0][numIters] == guess:
dummyList[position] = guess
del regularList[0][numIters]
numIters -=1
position +=1
numIters +=1
print regularList
print dummyList
print numIters
changeLetter(word)
Your program has one mistake when you delete an element the size of array becomes small and the element which should be next becomes previous.
regularList[0] = ['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k']
while guess in regularList[0]:
In this loop, when you remove first a , list becomes ['a', 'r', 'd', 'v', 'a', 'r', 'k']
Now when your loop continues, guess becomes 'r' that is next element in previous list. Hence a which was previously at position 1 is neglected (0 based indexing).
Maybe with enumerate and then remove the elements:
for index, element in enumerate(regularList):
if element == "a":
dummyList[index] = element
regularList.remove("a")
The accepted answer is way too long, you can just use a clean list comprehension
currentWord = [letter if letter in guessedLetters else "_" for letter in solution]
A fully functioning guess the word program without win logic would be 6 lines
solution = "aardvark"
guessedLetters = []
while True:
guessedLetters.append(input("Guess a letter "))
currentWord = [letter if letter in guessedLetters else "_" for letter in solution]
print(" ".join(currentWord))
This outputs:
Guess a letter a
a a _ _ _ a _ _
Guess a letter k
a a _ _ _ a _ k
Guess a letter v
a a _ _ v a _ k
Guess a letter r
a a r _ v a r k
Guess a letter d
a a r d v a r k
Guess a letter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.