我试图创造一个小游戏。
规则非常简单:你给出一个英文单词,计算机将试着逐个猜出这个单词。
问题是,我试图让计算机以聪明的方式猜出字母。让我举一个简单的例子说明我正在尝试构建的内容,以便您能够理解:
你给出了" cat"到电脑猜测。
我所拥有的130K单词列表缩小为仅包含3个字符的单词,仅限805个单词。从这个单词列表中,创建一个字母表,只包含25个字母(不是整个26个字母),因为新的805单词列表包含字母表中的所有字母,但是" z"。所以我们现在有一个包含25个(不同)字母的列表。
- 由于我无法在此处上传任何内容,我们将在此示例中说明,130K单词的大量列表是10个单词列表(变量名称&#34; fullDice&#34;) - < / p>
如果您尝试运行我的代码,请从此列表中选择一个单词,否则 它无法正常工作
计算机现在猜测这25个字母列表中的随机字母。
如果这封信中没有这封信,他就不会做任何事情并重新猜测一下这封信。
但如果字母在单词中,那就是事情变得更复杂。让我们说计算机猜字母&#34; c&#34;。我希望计算机重新缩小可能的单词列表,仅限于那些有&#34; c&#34;在第一个角色。这样,805字的列表现在成为唯一的36字列表。因为只有36个单词是3个字符并且以&#34; c&#34;开头,所以创建了一个新的字母表。而现在新的字母表只有14个字母,这使得计算机更容易猜出下一个字母,并对其进行正确判断。等等,直到找到所有的字母。
我被困在第5部分。如果您尝试在下面运行我的代码,您会看到字典列表从未缩小。这是我的问题。
import time
from random import randint
fullDice = ["panda", "tiger", "cat", "elephant", "whale", "leopard", "gorilla", "fish", "snake", "eagle"]
askForWord = input("Please enter an english word: ")
while True:
updatedDice = []
for k in range (0, len(fullDice)):
if len(askForWord) == len(fullDice[k]):
updatedDice += [fullDice[k]]
alphabet = []
for i in range (0, len(updatedDice)):
for n in range (0, len(updatedDice[i])):
if updatedDice[i][n] not in alphabet:
alphabet += [updatedDice[i][n]]
guessRandomLetter = alphabet[randint(0, len(alphabet) - 1)]
print("I guess the letter: " + guessRandomLetter)
print("From this dice: " + str(len(updatedDice)))
print("From this amount of letters: " + str(len(alphabet)) + "\n")
time.sleep(0.75)
guessedWordUnderlined = "_" * len(askForWord)
if guessRandomLetter in askForWord:
for m in range(0, len(askForWord)):
if askForWord[m] == guessRandomLetter: # CHECKING IF THE GUESSED LETTER IS INSIDE THE WORD
guessedWordUnderlined = list(guessedWordUnderlined)
guessedWordUnderlined[m] = guessRandomLetter
guessedWordUnderlined = ''.join(map(str, guessedWordUnderlined))
if guessedWordUnderlined == askForWord: # CHECK IF USER HAS WON
print("YOU WON")
break
答案 0 :(得分:0)
你刚问了一个包含该代码的qs。 我试着让它只能用你给出的字典中的可用单词作为&#34; python&#34;。
from random import randint
import random
import time
import datetime
random.seed(datetime.datetime.now())
wordOfTheUser = input("ENTER ENGLISH WORD HERE: ")
if wordOfTheUser in ("abracadabra", "python", "coding", "soup", "paper", "list", "leader", "program", "software", "eating","abcdefghigklmnopqrstuvwxyz"):
pass
else:
print("your word is not on the list, still devlopping.")
raise
diceList1 = ["abracadabra", "python", "coding", "soup", "paper", "list", "leader", "program", "software", "eating","abcdefghigklmnopqrstuvwxyz"]
diceList2 = []
for k in range (0, len(diceList1) - 1):
if len(diceList1[k]) == len(wordOfTheUser):
diceList2 += [diceList1[k]]
makeAlphabet = []
for b in range (0, len(diceList2)):
for x in range (0, len(diceList2[b])):
if diceList2[b][x] not in makeAlphabet:
makeAlphabet += [diceList2[b][x]]
computerWordSize = "_" * int(len(wordOfTheUser))
a= len(makeAlphabet)
while True:
try:
randomIndex = randint(0, a)
except ValueError:
randomIndex = randint(0, a)
pass
try:
letterChosenRandomly = makeAlphabet[randomIndex]
except IndexError as e:
try:
randomIndex = randint(0, int(len(makeAlphabet)))
letterChosenRandomly = makeAlphabet[randomIndex]
except:
pass
print("I guess the letter -> " + letterChosenRandomly)
diceList3 = []
if letterChosenRandomly in wordOfTheUser:
print("\n=== WON ===> " + letterChosenRandomly)
print("=== ALPHABET ===> " + str(len(makeAlphabet)))
print("=== HDW1 ===> " + str(len(diceList1)))
print("=== hdw2 ===> " + str(len(diceList2)))
print("=== hdw3 ===> " + str(len(diceList3)) + "\n\n")
k=-1
makeAlphabet = []
for i in range (0, len(wordOfTheUser) ):
if letterChosenRandomly == wordOfTheUser[i]:
computerWordSize = list(computerWordSize)
computerWordSize[i] = letterChosenRandomly
for l in range (0, len(diceList2)):
if computerWordSize[i] == diceList2[l][i]:
diceList3 += [diceList2[l]]
for d in range(0, len(diceList3)):
for h in range(0, len(diceList2[b])):
if diceList2[d][h] not in makeAlphabet:
makeAlphabet += [diceList2[d][h]]
won = False
computerWordSize = ''.join(map(str, computerWordSize))
print(computerWordSize)
if computerWordSize == wordOfTheUser:
won = True
if won is True:
print("YOU WON")
break
time.sleep(1)
else:
print("\n=== LOOSE ===> " + letterChosenRandomly)
print("=== ALPHABET ===> " + str(len(makeAlphabet)))
print("=== HDW1 ===> " + str(len(diceList1)))
print("== hdw2 ===> " + str(len(diceList2)))
print("=== hdw3 ===> " + str(len(diceList3)) + "\n\n")
try:
makeAlphabet.remove(letterChosenRandomly)
except:
print ("Letters not in list")
break
k=0
diceList3 = []
for q in range (0, len(wordOfTheUser) - 1):
for l in range(0, len(diceList2)):
if computerWordSize[q] == diceList2[l][q]:
diceList3 += [diceList2[l]]
for d in range(0, len(diceList3)):
for h in range(0, len(diceList2[b])):
try:
if diceList2[d][h] not in makeAlphabet:
makeAlphabet += [diceList2[d][h]]
except:
try:
for s in range(0, len(diceList3)):
for f in range(0, len(diceList2)):
if diceList2[s][f] not in makeAlphabet:
makeAlphabet += [diceList2[s][f]]
except:
("your word is too short")
time.sleep(1)
答案 1 :(得分:0)
我认为问题是if guessedWordUnderlined in askForWord
永远不会成真。 in
运算符测试第一个运算符是否在第二个参数内,这是一个容器,例如列表或字符串。 "_" * len(askForWord)
的值guessedWordUnderlined
是一串下划线,您正在测试它是否在askForWOrd
中。如果askForWord
的值为cat
,askForWord
可以被视为["c", "a", "t"]
,那么in运算符将测试"___" == "c" or "___" == "a" or "___" == "t"
,其中任何一个都不会真正。这意味着它下面的代码永远不会执行,因此代码会永远重复,随机猜测单词中的字母。我无法确切知道这个if
的功能是什么,因为你已经知道你可以选择的每个字母都在askForWord
,但我确信我错过了一些明显的东西。
顺便说一下,你经常使用类似于for x in range(0, len(LIST): ... LIST[x]
的结构,它可以更简洁,更明显地写成for x in LIST: ... x
。例如,您的代码
for k in range (0, len(fullDice)):
if len(askForWord) == len(fullDice[k]):
updatedDice += [fullDice[k]]
alphabet = []
可以写成
for k in fullDice:
if len(askForWord) == len(k):
updatedDice += [k] # This should be updatedDice.append(k), but I
# have left it as-is for simplicity's sake.
alphabet = []
这应该可以帮助您的代码更具可读性。还有一些其他的编辑可以使你的代码更加pythonic,但除了这一行,我不能看到任何功能上的错误。如果你分享这个if
应该提供的帮助,可能会让你在代码中找到任何其他错误变得容易一些。我希望这有帮助,祝你好运!
答案 2 :(得分:0)
再次审核了您的代码后,我认为问题出在guessedWordUnderlined = "_" * len(askForWord)
语句中。这会创建一个下划线字符串,其长度等于askFOrWord
的长度。问题是每个while True:
循环的迭代中都有一个 new 字符串。这意味着在每次迭代时,字符串将变为带有一个正确字母的下划线列表,但在下一次迭代中,它将被覆盖。要解决此问题,您应该将行guessedWordUnderlined = "_" * len(askForWord)
从当前位置移至askForWord = input("Please enter an english word: ")
正下方。这意味着它存在于全局范围而不是本地范围,这意味着它不会被覆盖。如果我没有弄错的话,您还应该将行global guessedWordUnderlined
放在while
循环的开头。这可能需要您重做一些代码。希望这对你有用!