【问题标题】:Hangman game what to put to keep code running刽子手游戏放什么来保持代码运行
【发布时间】:2018-02-04 21:34:35
【问题描述】:

这是我的刽子手游戏代码。我不知道如何检查他们输入的字母是否在单词中,然后检查字母序列是否等于单词中的字母。

感谢您的帮助。

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! :(  ")

【问题讨论】:

  • 请更具体地说明您遇到问题的部分。您的代码中“出了什么问题”。为我们缩小您的问题范围,以便我们作为志愿者知道从哪里开始寻找,并且实际上有动力去寻找。请查看:在发布您的下一个问题之前,请阅读并遵循以下文档:stackoverflow.com/help/how-to-askstackoverflow.com/help/mcve,然后是 edit 您的问题。感谢您帮助我们。

标签: python-3.6


【解决方案1】:

我不明白您所说的“检查字母序列是否等于单词中的字母”的意思。请举例说明您希望在这方面实现的目标。

至于检查'字母'是否在被猜的'单词'中,你可以简单地用这段代码来做:

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 一样简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2014-10-19
    相关资源
    最近更新 更多