我前天开始编写(在
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”,因为输入部分的一切都很好,它只是不应该停止它! (意思是什么时候应该没什么可猜的!)
所以我希望有人能够浪费两天的时间来帮助我解决我的基本 – 不那么有趣的问题!提前致谢!
最佳答案 >您可以拥有多个字母的变量名称
> 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()))