我正在开发文字游戏。用户的目的是在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个字母的单词,它仍会打印出该单词。虽然我宁愿认为这个词只是被忽略了,并且没有尝试使用..
如果给出正确的字母(以及第一个字母),它不会成为反馈的标准部分。试图用温度来解决这个问题,但是它的效果并不好。
我们也欢迎任何清理我的代码的建议!
感谢您的关注
答案 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)
回答您的具体问题:
for
周围设置input
循环,让用户保持在该循环中,直到他们输入适当长度的单词"abcde"
然后"fghij"
等来赢取是微不足道的。您需要仔细考虑您的规则是什么;你可以有一个单独的列表“猜测中的字母在答案中,但在错误的地方”并向用户显示此信息。 display = ["." for letter in answer]
,并随时更新。 您遇到的其他问题:
len("plural") != 5
);你应该重写你的代码使用单词的长度(这使它更灵活)。 if all(letter != "." for letter in display):
,看看他们是否有这样的答案。 [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)