这是我的刽子手游戏代码。我无法弄清楚如何检查他们输入的字母是否在单词中,然后检查字母序列是否等于单词中的字母。
感谢您的帮助。
import sys
import random
answers = [ a long list of possible answers all correctly formatted :d ]
again =""
num = 0
win = True
while again == "":
contin = input("""
...Hangman...
enter to play
['stop'] to stop
""")
again = contin.title()
if again == "":
num = random.randint(0,len(answers)-1)
word = answers[num]
tries = len(word)+10
print(word)
while :
guessmessage ="guess a letter.... You have ",tries,"tries: "
guess = input(guessmessage)
if tries != len(word)+10:
for letter in word:
if guess != letter:
print(" ")
tries = tries - 1
else:
print(letter)
tries = tries - 1
else:
sys.exit("You Have Ran Out Of Attempts..... Better Luck Next Time!")
else:
sys.exit("You Have Quit The Game! :( ")
答案 0 :(得分:0)
我不明白你的意思“检查字母序列是否等于单词”中的字母。请举例说明您希望在该部分实现的目标。
至于检查'字母'中的'字母'是否在猜测中,您可以使用以下代码执行此操作:
answer = "hangman"
guess = 'a'
if guess in word:
print("Letter", guess, "is in the answer.")
else:
print("Letter", guess, "is not in the answer.)
我只是使用if-else作为示例,但基本上语法与letter in word
一样简单。