简体   繁体   English

在 python 中使用循环为 HANGMAN 游戏定义一个函数

[英]defining a function for the HANGMAN game using a loop in python

i am a beginner in python and would appreciate your help.我是 python 的初学者,非常感谢你的帮助。

I need to define a function(part of the HANGMAN GAME):我需要定义一个函数(HANGMAN GAME 的一部分):

def show_hidden_word(secret_word, old_letters_guessed):

secret_word = a string of the word the player needs to guess. secret_word = 玩家需要猜测的单词字符串。 old_letters_guessed = a list that contains the guessed letters by the player thus far. old_letters_guessed = 一个包含玩家迄今为止猜测的字母的列表。

the function needs to return a string made from letters and ' _ '.该函数需要返回一个由字母和'_'组成的字符串。 the string shows the letters from the old_letters_guessed list that are in the secret_word string in their right place(index), and the rest of the letters the player has not guessed yet as a ' _ '.该字符串显示old_letters_guessed列表中位于secret_word字符串中正确位置(索引)的字母,以及玩家尚未猜到的其余字母为“_”。

its supposed to look like this:它应该看起来像这样:

>>> secret_word = "mammals"
>>> old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k']
>>> print(show_hidden_word(secret_word, old_letters_guessed))
m _ m m _ _ s

this is what ive tried to do and it doesn't work (has to be done using for/ while loop):这是我试图做的,但它不起作用(必须使用 for/while 循环来完成):

def show_hidden_word(secret_word, old_letters_guessed):
 clue = ""
 for letter in secret_word:
  if letter in clue == True:
   clue + letter
   clue = clue + letter
  else:
   clue + '_'
   clue = '_'+ clue
 return clue

Thank you very much!非常感谢!

This should do the trick:这应该可以解决问题:

def show_hidden_word(secret_word, old_letters_guessed):
  return " ".join([c if c in old_letters_guessed else "_" for c in secret_word])

secret_word = "mammals"
old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k']
print(show_hidden_word(secret_word, old_letters_guessed))

There are a few issues in your code:您的代码中有几个问题:

  • clue = '_' + clue should be clue = clue + '_' , since you want to append the underscore, not prepend it. clue = '_' + clue应该是clue = clue + '_' ,因为你想附加下划线,而不是前面。 You did it right for the other case, where you have clue = clue + letter你在另一种情况下做得对,你有clue = clue + letter

  • The if condition is wrong in two ways: if条件在两个方面是错误的:

    • It should not have the comparison with True , as this will first compare the string with True , and only then perform the in operator.它不应该与True进行比较,因为这将首先将字符串与True进行比较,然后才执行in运算符。
    • You don't want to check whether the letter is in clue (which is the result you are building), but whether it is in the old guesses.您不想检查这封信是否在clue中(这是您正在构建的结果),但是否在旧猜测中。

    So it should be: if letter in old_letters_guessed:所以应该是: if letter in old_letters_guessed:

  • Not a blocking issue, but the stand-alone expressions clue + letter and clue + '_' do nothing useful.不是阻塞问题,但独立表达式clue + letterclue + '_'没有任何用处。 These lines of code can be removed.可以删除这些代码行。

If you fix those issues, your code will work.如果您解决了这些问题,您的代码将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM