字符串中的Foreach字符-python

时间:2019-07-01 16:20:38

标签: python

我比Python更习惯C#。闲暇之余,我决定用Python创建一个简单的hangman控制台应用程序。玩家1输入了一个单词,然后玩家2尝试了5次猜测。假设他们猜后说“ hello”,则说“ d”将显示_ _ _ _ _。如果他们的猜测是“ e”,它将打印_ e _ _ _。

由于某种原因,无论我猜字母是正确还是不正确,它只会显示一个_

word = input("Player 1, please enter a word: ")
lives = 5
print("Player 2, you have {} lives left.".format(lives))
print("The Word: ", "_ " * len(word))
wordSuccessfullyGuessed = False
while lives > 0 or wordGuessed: 
    guess = input("Player 2, guess a letter: ")
    wordFormatted = ""
    for char in word:
        if char in guess:
            wordFormatted = char + " "
        else:
            wordFormatted = "_ "

    print(wordFormatted)



5 个答案:

答案 0 :(得分:1)

在内部for循环中,您将在每次迭代中重新分配变量wordFormatted。看起来您想将字母或下划线附加到字符串中。请尝试:

for char in word:
        if char in guess:
            wordFormatted += char + " "
        else:
            wordFormatted += "_ "

看来,您在while循环的每次迭代中都在重新分配wordFormatted = ""。最终,每次猜测都会清除单词。可能也想看看。

答案 1 :(得分:1)

word = input("Player 1, please enter a word: ").lstrip().rstrip().lower()
wordBlank = ['_ ' for char in word]
lives = 5
wordLen = len(word)
wordSuccessfullyGuessed = False
wordFormatted = ""
wordGuessed = False
while lives > 0 or wordGuessed:
    guess = ''
    validGuess = False
    print("Player 2, you have {} lives left.".format(lives))
    while len(guess) != 1 or guess.isalpha() == False:
        guess = input('Enter one letter to guess: ').lower()
        characterIteration = 0
        for char in word:
            if guess == char:
                validGuess = True
                wordBlank[characterIteration] = guess+' '
            characterIteration += 1
    if validGuess == False:
        lives -= 1
    print("The Word: ", ''.join(wordBlank))
    if '_ ' not in wordBlank:
        wordGuessed = True
if wordGuessed == True:
    print('You won! The word was indeed', word)
if wordGuessed == False:
    print("You didn't win this time, the word was",word)

输出:

Player 1, please enter a word: trees
The Word:  _ _ _ _ _
Player 2, you have 5 lives left.
Enter one letter to guess: a
The Word:  _ _ _ _ _
Player 2, you have 4 lives left.
Enter one letter to guess: e
The Word:  _ _ e e _
Player 2, you have 4 lives left.
Enter one letter to guess: t
The Word:  t _ e e _
Player 2, you have 4 lives left.
Enter one letter to guess: r
The Word:  t r e e _
Player 2, you have 4 lives left.
Enter one letter to guess: s
The Word:  t r e e s
You won! The word was indeed trees

答案 2 :(得分:0)

Carcigenicate mentions in a comment

  

wordFormatted =“ _”看起来像是一个可能的嫌疑人。我认为您的意思是wordFormatted + =“ _” –

使用此解决了问题。

答案 3 :(得分:0)

这里

def hangman_print(guess_lst, word):
    lst = []
    for x in word:
        lst.append(x if x in guess_lst else '_')
    print(''.join(lst))


hangman_print(['l', 't'], 'telescope')

输出

t_l______

答案 4 :(得分:0)

您不需要遍历单词中的每个字符。内置的in用于字符串时,已经可以处理检查字符串中是否包含子字符串。

word = input("Player 1, please enter a word: ")
lives = 5
print("Player 2, you have {} lives left.".format(lives))
print("The Word: ", "_ " * len(word))
wordSuccessfullyGuessed = False
guessed = []
while lives > 0 or wordGuessed: 
    guess = input("Player 2, guess a letter: ")
    guessed.append(guess) # This is what you want to track, what letters they've already guessed
    wordFormatted = ''.join([letter + ' ' if letter in guessed else '_ ' for letter in word])

    print(wordFormatted)

我用于wordFormatted的列表理解将为您提供预期的输出,其中未猜测的字符显示为'_',正确猜测的字符(包括重复的字母)将显示给猜测者。

Player 1, please enter a word: apple
Player 2, you have 5 lives left.
The Word:  _ _ _ _ _
Player 2, guess a letter: a
a _ _ _ _
Player 2, guess a letter: p
a p p _ _
Player 2, guess a letter: e
a p p _ e
Player 2, guess a letter: s
a p p _ e