Python 3.x 尝试替换列表中的项将删除列表的其余部分
我目前正在用python编写一个刽子手游戏,这是我在空白行中添加字符的代码Python 3.x 尝试替换列表中的项将删除列表的其余部分,python-3.x,list,Python 3.x,List,我目前正在用python编写一个刽子手游戏,这是我在空白行中添加字符的代码 if guess in correct: replace = int(correct.index(guess)) guesslist[:replace] = guess print(guesslist) 这种方法在替换第一个字符时有效 acceptabel Gissa en bokstav: a ['a', ' _ _ _ _ _ _ _ _ _ _ '] 但每当我尝试替换不是第一个的字
if guess in correct:
replace = int(correct.index(guess))
guesslist[:replace] = guess
print(guesslist)
这种方法在替换第一个字符时有效
acceptabel
Gissa en bokstav: a
['a', ' _ _ _ _ _ _ _ _ _ _ ']
但每当我尝试替换不是第一个的字符时,就会发生这种情况:
minus
Gissa en bokstav: n
['n']
我该如何解决这个问题
编辑:完整代码
import random
dead = int(0)
tried = list()
with open("ord.txt", "r") as file:
data = file.read()
words = data.split()
# Generating a random number for word position
word_pos = random.randint(0, len(words)-1)
print(words[word_pos])
correct = list(words[word_pos])
guesslist = list()
guesslist.append(' _ ' * len(correct))
guess = input('Gissa en bokstav: ')
if guess in correct:
replace = int(correct.index(guess))
guesslist[:replace] = guess
print(guesslist)
else:
tried.append(guess)
print(tried)
if dead == 9:
print('_____ \n| |\n| 0\n| /|\ \n| / \ \nL_______')
print('BRUH HE DEAD')
elif dead == 8:
print('_____ \n| |\n| 0\n| /|\ \n| / \nL_______')
elif dead == 7:
print('_____ \n| |\n| 0\n| /|\ \n| \nL_______')
elif dead == 6:
print('_____ \n| |\n| 0\n| /| \n| \nL_______')
elif dead == 5:
print('_____ \n| |\n| 0\n| | \n| \nL_______')
elif dead == 4:
print('_____ \n| |\n| 0\n| \n| \nL_______')
elif dead == 3:
print('_____ \n| |\n| \n| \n| \nL_______')
elif dead == 2:
print('_____ \n| \n| \n| \n| \nL_______')
elif dead == 1:
print(' \n| \n| \n| \n| \nL_______')
elif dead == 0:
print('\n \n \n \n \n________')
这是我将采取的方法: 鉴于:
为了帮助澄清您的问题,如果在正确的索引中找到字符,您希望在猜测列表中适当的正确索引处插入一个新字符(猜测)。是的,这就是我试图做的。不确定如何将其集成到我的代码中,我已将我的整个程序添加到问题中,以获得额外的上下文(正确的单词?)。
correct = 'happily'
answer = [" "]*len(correct)
def update_answer(cor, ans, g):
if g in cor:
st = 0
while st < len(cor):
indx = cor.find(g, st)
if indx < 0:
break
ans[indx] = g
st = indx+1
return ans
import random as rnd
def hangman():
solved = False
max_guesses = 4
good_guesses = 0
correct = ""
tried = []
# Get new random word
with open("ord.txt", "r") as file:
data = file.read()
words = data.split()
# Make sure correct is composed of Upper case letters
correct = rnd.choice(words).upper()
correct = rnd.choice(test_words).upper()
answer = [""] * len(correct)
while len(tried) < max_guesses and not solved:
# make sure guess is single letter and upper case
guess = input('Gissa en bokstav: ')[0].upper()
if guess in answer or guess in tried:
print (f"You have already guessed the letter {guess}, try again!")
elif guess in correct:
st = 0
while st < len(correct):
indx = correct.find(guess, st)
if indx < 0:
break
answer[indx] = guess
good_guesses += 1
st = indx+1
if len(correct) == good_guesses:
print('Congratulations You have solved the Puzzle')
solved = True
print (f"Answer: {answer} = {correct}" )
else:
print (f'Good Guess\nAnswer Status = {answer}')
else:
tried.append(guess)
print (f'Incorrect Guess: {tried}')
print (f"You have {max_guesses - len(tried)} guesses left")
if not solved:
print(f"Sorry you failed to guess {correct}")