我正在一个文字游戏工作。用户的目的是在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个字母的单词,它仍然会打印这个单词。虽然我宁愿让这个词被忽略,并且这个尝试没有被使用。
如果一个字母给出正确(也是第一个字母),它就不会成为反馈的标准部分。试图得到这个温度,但它的工作效果不好。
任何建议来清理我的代码也感激!
感谢您的关注
@ zord。哇,这是伟人,非常感谢!我的第一个版本也包含“while”,但后来改变了。 '最后猜测','效果更好。谢谢,我很感激!欢呼声 – user3119123
欢迎您! :) – zord