python改进文字游戏

时间:2013-12-22 12:37:53

标签: python

我正在开发文字游戏。用户的目的是在5次尝试中猜出一个5个字母的单词。用户可以知道第一个字母。如果他没有说出正确的话,但如果他在正确的地方有一封信,他就会知道这一点。

这是我的代码:

  import random
list_of_words = ["apple","table", "words", "beers", "plural", "hands"]
word = random.choice(list_of_words)

attempts = 5

for attempt in range(attempts):
    if attempt == 0:
        tempList = list(word[0] + ("." * 4))
        print("The first letter of the word we are looking for: %s" % "".join(tempList))

    answer = raw_input("What is the word we are looking for?:")
    if len(answer) != 5:
        print ('Please enter a 5 letter word')

    Else:
        if answer != word:
            wordlist = list(word)
            answerlist = list(answer)
            for i in range(min(len(wordlist), len(answerlist))):
                if wordlist[i] == answerlist[i]:
                    tempList[i] = wordlist[i]
            print(tempList)

        else:
            print("correct, you have guessed the word in:", attempt, "attempts")

if answer != word:
    print("Sorry maximum number of tries, the word is: %s" % word)

我对此代码有两个问题:

第一个是一个小问题:如果用户给出一个6或4个字母的单词,它仍会打印出该单词。虽然我宁愿认为这个词只是被忽略了,并且没有尝试使用..

如果给出正确的字母(以及第一个字母),它不会成为反馈的标准部分。试图用温度来解决这个问题,但是它的效果并不好。

我们也欢迎任何清理我的代码的建议!

感谢您的关注

3 个答案:

答案 0 :(得分:1)

代码有几个问题。

现在只需1。我注意到在示例输出中您输入了五个字母单词(beeds和bread),它仍打印出Please enter a 5 letter word

这两行:

if len(answer) != 4:
    print ('Please enter a 5 letter word')

当然应该是:

if len(answer) != 5:
    print ('Please enter a 5 letter word')
    continue

这会捕获无效输入并再次循环。

答案 1 :(得分:1)

回答您的具体问题:

  1. 您需要在for周围设置input循环,让用户保持在该循环中,直到他们输入适当长度的单词
  2. 如果您将猜测的字母移动到正确的位置,那么通过猜测"abcde"然后"fghij"等来赢取是微不足道的。您需要仔细考虑您的规则是什么;你可以有一个单独的列表“猜测中的字母在答案中,但在错误的地方”并向用户显示此信息。
  3. 要使显示版本保留所有以前猜到的字符,请保留显示字符列表:display = ["." for letter in answer],并随时更新。
  4. 您遇到的其他问题:

    1. 字长的硬编码太多(特别是len("plural") != 5);你应该重写你的代码使用单词的长度(这使它更灵活)。
    2. 如果用户猜出整个答案,你只会告诉用户他们已经赢了。如果他们用重叠的字母来表达怎么办?你可以测试为if all(letter != "." for letter in display):,看看他们是否有这样的答案。
    3. 您的列表理解[i for i in answer if answer in word]永远不会分配给任何内容。

答案 2 :(得分:1)

我对您的代码进行了一些更改,现在它正在根据您的规范进行操作。我还在其中写了几个解释性评论:

import random

list_of_words = ["apple", "table", "words", "beers", "plural", "hands"]
word = random.choice(list_of_words)

# changed the loop to a 'while', because i don't want to count the invalid length answers
# and wanted to exit the loop, when the user guessed correctly
attempts = 5
attempt = 0
correct = False
while attempt < attempts and not correct:
    if attempt == 0:
        # i stored a working copy of the initial hint (ex: "w....")
        # i'll use this to store the previously correctrly guessed letters
        tempList = list(word[0] + ("." * 4))
        print("The first letter of the word we are looking for: %s" % "".join(tempList))

    answer = raw_input("What is the word we are looking for?:")
    if len(answer) != 5:
        print("Please enter a 5 letter word")
    else:
        if answer != word:
            # i simplified this loop to comparing the wordlist and answerlist and update templist accordingly
            wordlist = list(word)
            answerlist = list(answer)
            for i in range(min(len(wordlist), len(answerlist))):
                if wordlist[i] == answerlist[i]:
                    tempList[i] = wordlist[i]
            print(tempList)
        else:
            correct = True
            print("Correct, you have guessed the word in %s attempts" % (attempt + 1))
        attempt += 1

if answer != word:
    # also i used string formatting on your prints, so is prints as a string, and not as a tuple.
    print("Sorry maximum number of tries, the word is: %s" % word)