2013-12-22 55 views
0

我正在一个文字游戏工作。用户的目的是在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个字母的单词,它仍然会打印这个单词。虽然我宁愿让这个词被忽略,并且这个尝试没有被使用。

如果一个字母给出正确(也是第一个字母),它就不会成为反馈的标准部分。试图得到这个温度,但它的工作效果不好。

任何建议来清理我的代码也感激!

感谢您的关注

回答

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) 
+0

@ zord。哇,这是伟人,非常感谢!我的第一个版本也包含“while”,但后来改变了。 '最后猜测','效果更好。谢谢,我很感激!欢呼声 – user3119123

+0

欢迎您! :) – zord

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. 你需要让你的input围绕for循环,保持用户在该循环,直到他们进入适当的长度
  2. 的话如果你移动猜字母正确的地方,通过猜测"abcde"然后"fghij"等来赢得微不足道。你需要仔细考虑你的规则是什么;你可以有一个单独的列表“在猜中的字母在答案中,但在错误的地方”并向用户显示。
  3. 要保留所有以前猜到的字符的显示版本,请保留显示字符列表:display = ["." for letter in answer],并随时更新。

其他问题,您有:

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