如何替换隐藏的信挂人

时间:2016-11-27 22:58:57

标签: python

如果你有话,那我试着把它弄到一个地方#34; man"例如它看起来像_ _ _。如果用户输入" m"它看起来像m _ _。我知道我的问题在于#34;#用户将在哪里输入猜测"在for循环下发表评论 随机导入

user_input = ""
turns = 5

# List of words
print("Welcome to Advanced Hang Man!")
guesses = ["hello"]

# Picks a random word from the list and prints the length of it
random_guesses = (random.choice(guesses))
right_guess = []
wrong_guess = []

# Prints the hidden word in "_" format
hidden_word = "_" * len(random_guesses)
print(hidden_word)

# Where user will type guess
while True:
    user_input = input("Please enter a letter once at a time:")
    user_input = user_input.lower()
    for i in range(len(random_guesses)):
        if user_input == random_guesses[i]:
            print(hidden_words)

2 个答案:

答案 0 :(得分:0)

您需要迭代实际单词并检查right_guesses列表中的字符。如果找不到,请用新单词中的_替换该字符。以下是实现此目的的示例代码:

>>> my_word = "StackOverflow"
>>> right_guesses = ['s', 'o', 'c']
>>> ' '.join([word if word.lower() in right_guesses else '_'  for word in my_word])
'S _ _ c _ O _ _ _ _ _ o _'

答案 1 :(得分:0)

这是我尝试制作游戏的方法。我没有使用您的right_guesses列表,也没有使用wrong-guesses,但它具有悬挂式游戏的基本功能:

user_input = ""

print("Welcome to Advanced Hang Man!")

random_word= 'bicycle'

hidden_word = "_" * len(random_word)

going = True

while going:
    print(hidden_word)
    user_input = input("Please enter a letter once at a time:");
    user_input = user_input.lower()
    for i in range(len(random_word)): 
      if user_input == random_word[i]:
        print ('Letter found!')
        temp = list(hidden_word)
        temp[i] = user_input
        hidden_word = ''.join(temp)
        if (hidden_word == random_word):
          print ('You Won!!! The word was ' + random_word)
          going = False