初学者刽子手游戏:无法使其发挥作用

时间:2017-06-21 16:58:29

标签: python

我前天开始编写(在Python 3上)(这使我成为一个严重的新手)我发现我会尝试制作自己的Hangman游戏,但我只是不知道什么是错的到目前为止我做了什么! ^ _ ^就是这样:

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
a = input()
tries = 1
print(W)
while len(W) != 0 and tries<10:
    if a in W:
        print("yes")
        W.remove(a)
        tries += 1
        a = input()
    elif a not in W:
        print("no") 
        tries += 1
        a = input()
else:
    if len(W) == 0: 
        print("Well done! Your word was")
        print(w) 
    elif tries == 10:
        print("You died!")

我认为问题来自于我的循环事物“而len(W)!= 0”,因为输入部分的一切都很好,它只是不应该停止它! (意思是什么时候应该没什么可猜的!) 所以我希望有人能够浪费两天的时间来帮助我解决我的基本 - 不那么有趣的问题!提前致谢!

3 个答案:

答案 0 :(得分:1)

  • 您可以拥有多个字母的变量名称

  • random.choice(L)L[random.randrange(len(L))]

  • 更容易

所以

from random import choice

def show_word(target_word, remaining_letters, blank="-"):
    print("".join(blank if ch in remaining_letters else ch.upper() for ch in target_word))

words = ["cat", "dog", "rabbit"]
target_word = choice(words)
remaining_letters = set(target_word)

print("Let's play Hangman!")

for round in range(1, 11):
    show_word(target_word, remaining_letters)
    guess = input("Guess a letter: ").strip().lower()
    if guess in remaining_letters:
        print("You got one!")
        remaining_letters.remove(guess)
        if not remaining_letters:
            break
    else:
        print("Sorry, none of those...")

if remaining_letters:
    print("You died!")
else:
    print("You solved {}! Well done!".format(target_word.upper()))

答案 1 :(得分:0)

当您猜到上一个循环结束时的最后一个字母时,当前循环将告诉您猜测是否正确,然后要求另一个字母。尝试更像这样的东西

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
tries = 0
print(W)
while len(W) != 0 and tries<10:
    a = input()
    if a in W:
        print("yes")
        W.remove(a)
        tries += 1
    elif a not in W:
        print("no") 
        tries += 1
else:
    if len(W) == 0: 
        print("Well done! Your word was")
        print(w) 
    elif tries == 10:
        print("You died!")

答案 2 :(得分:0)

如果我理解你的问题,你可以像这样解决你的问题,还记得你可以给用户提供信息在输入法中添加提示:

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
tries = 1
print(W)
while len(W) != 0 and tries<10:
  a = input("select word")
  if a in W:
    print("yes")
    W.remove(a)
    tries += 1
  else:
    print("no") 
    tries += 1

  if len(W) == 0: 
    print("Well done! Your word was")
    print(w)
  elif tries == 10:
    print("You died!")