目前,我一直致力于创建一个刽子手游戏,并且我设法让大部分脚本正常运行并且运行得很好但是,我在使用列表中的列表方面遇到了问题我想列出一些"用过的字母"这会阻止你再次使用同一个字母。我发现的问题是我要么设置它,所以它总是包含猜测变量而不是输入,或者它只是不起作用而且我得到语法错误。关于我应该如何做的任何建议?
下面,您将找到代码:
from random import randint
ans = input(">>>>>Let's play the word guessing game!<<<<<")
ans_upper = ans.upper()
if ans_upper == "YES":
print("Good because that's what we're going to play!")
else:
print("Tough, because we're going to play hangman anyways!")
#---#---#---#---#---#---#---#---#---#---# RANDOM WORD #---#---#---#---#---#---#---#
x = randint(1,15)
if x == 1:
word = "bubblegum"
elif x == 2:
word = "pasta"
elif x == 3:
word = "cow"
elif x == 4:
word = "chicken"
elif x == 5:
word = "milk"
elif x == 6:
word = "chair"
elif x == 7:
word = "computer"
elif x == 8:
word = "psychology"
elif x == 9:
word = "clock"
elif x == 10:
word = "melon"
elif x == 11:
word == "word"
elif x == 12:
word = "brackets"
elif x == 13:
word = "hangman"
elif x == 14:
word = "paper"
elif x == 15:
word = "internet"
else:
print("ERROR: NUMBER HAS EXCEEDED THE VALUE OF 15")
#---#---#---#---#---#---#---#---#---# MYSTERY LETTERS #---#---#---#---#---#---#--
blanks = list("-"*len(word))
print("Can you guess the word, there are " , len(word), " letters!")
print(''.join(blanks))
word_list = list(word)
#---#---#---#---#---#---#---#---#---# LOOPING #---#---#---#---#---#---#---#---#---
loop = True
finished = len(word) * 1
done = 1
used = [] #This is for the used letters
while loop == True:
guess = str(input("Enter a letter!"))
checker = len(guess)
if checker == 1:
for letter_index in range(len(word_list)):
if guess in word_list[letter_index]:
guess_index = word_list.index(guess)
letter_index = guess_index
blanks[guess_index] = guess
done = done + 1
print("".join(blanks))
if done == finished:
print("You win!")
loop = False
break
elif checker > 1:
print("Hey, guess one letter at a time!")
因此,我希望分配给存储使用过的字母的任务的区域标记为&#34;使用&#34;。我知道它涉及.append()
命令,但我不知道它去哪里以及我是如何写出命令的。
提前致谢,
理查德
答案 0 :(得分:0)
我认为你需要了解两个花絮。首先,追加是你想要使用的新猜测:
used = []
guess = 'x'
used.append(guess)
print used
[&#39; X&#39;]
其次,要查看猜测是否已被使用,请在&#39;中使用&#39;
print guess in used
真
所以你可能想做这样的事情:
if guess in used:
print "You already guessed that!"
else:
used.append(guess)
...