我目前正在使用python开发MIT Opencourseware,其中一项任务是制作Hangman游戏。
我设法完成的大多数功能都做得很好,但是我遇到的问题在于这两个功能:
def get_guessed_word(secret_word, letters_guessed):
lengthOf = len(secret_word)
listLength = ["_ "] *lengthOf
for i,char in enumerate(secret_word):
if char == letters_guessed:
listLength[i]=char+" "
listCopy = listLength[:]
print(list)
def get_available_letters(letters_guessed):
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i, char in enumerate(alphabet):
if char == letters_guessed:
alphabet[i]="_"
alphabetCopy = alphabet[:]
print(alphabetCopy)
break
问题是,每次我通过该函数时,字母都会重置,并尝试通过创建副本来解决此问题,但我意识到该解决方案甚至在实现之前就真的不起作用,因为listCopy和AlphabetCopy只是复制了“零”状态”,则每次调用该函数。
我知道我可以做其他解决方案,但是我特别想要这种“用户体验”。我尝试了其他解决方法,但现在无法解决。
答案 0 :(得分:0)
因此,您有一个列表,其中包含已经被猜测的字母,并且您想知道剩下的字母。
最简单的方法是使用sets
。
def get_available_letters(guessed_letters):
alphabet = set(map(chr, range(97, 123))) # Same list like you but shorter version
return sorted(alphabet - set(guessed_letters))
这是什么:
>>>get_available_letters(['a', 'e', 'f'])
['b', 'c', 'd', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def get_guessed_word(secret_word, letters_guessed):
guessed_word = ["_"] * len(secret_word)
for i, letter in enumerate(secret_word):
if letter in letters_guessed: # Changed == to in
guessed_word[i] = letter # Don't break after a letter was found and no copy necessary
return "".join(guessed_word)
>>>get_guessed_word("Hello", ["e", "o"])
'_e__o'
答案 1 :(得分:0)
我假设变量letters_guessed是一个包含所有被猜出字母的列表或集合。
在这种情况下,您可以使用:
def GetGuessedWord(SecretWord, GuessedLetters):
Ln = len(SecretWord)
DisplayList = ["_ "]*Ln
for i, char in enumerate(SecretWord):
if char in GuessedLetters: # This will check if char is present in the list
DisplayList[i] = char + " "
print(DisplayList)
def GetAvailableLetters(GuessedLetters):
Letters = "abcdefghijlkmnopqrstuvwxyz"
DisplayList = [L for L in Letters] # Converts it into a list of smaller strings, 1 letter each
for i, char in enumerate(DisplayList):
if char in GuessedLetters:
DisplayList[i] = "_"
print(DisplayList)
>>> GetGuessedWord("overgrown", ['o'])
['o ', '_ ', '_ ', '_ ', '_ ', '_ ', 'o ', '_ ', '_ ']
>>> GetGuessedWord("overgrown", ['o', 'e', 'r'])
['o ', '_ ', 'e ', 'r ', '_ ', 'r ', 'o ', '_ ', '_ ']
>>> GetGuessedWord("overgrown", ['o','e','r','z'])
['o ', '_ ', 'e ', 'r ', '_ ', 'r ', 'o ', '_ ', '_ ']
>>> GetAvailableLetters(['o'])
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'k', 'm', 'n', '_', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> GetAvailableLetters(['o', 'e', 'r'])
['a', 'b', 'c', 'd', '_', 'f', 'g', 'h', 'i', 'j', 'l', 'k', 'm', 'n', '_', 'p', 'q', '_', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> GetAvailableLetters(['o', 'e', 'r', 'z'])
['a', 'b', 'c', 'd', '_', 'f', 'g', 'h', 'i', 'j', 'l', 'k', 'm', 'n', '_', 'p', 'q', '_', 's', 't', 'u', 'v', 'w', 'x', 'y', '_']
如果要比较的对象具有相同的值,则'=='运算符将返回true。因为一个是字符串,另一个是列表,所以它将返回false。
如果左侧输入出现在右侧输入中,则“ in”运算符将返回true。
如果您一次猜一个字母,则需要使用函数对外部变量进行永久修改。因此,可变字母不能在get_available_letters中声明,而必须在主代码中声明,并作为输入传递给get_available_letters函数。如果letters_guessed是一个长度为1个字母的字符串,则应该可以修复此功能。现在,您可以使用'=='运算符。
def get_available_letters(letters_guessed, alphabet):
for i, char in enumerate(alphabet):
if char == letters_guessed:
alphabet[i]="_" # This line will permanently change the variable alphabet
alphabetCopy = alphabet[:] # Not useful, u may as well print the original
print(alphabetCopy)
break
>>> alphabet = [L for L in 'abcdefghijklmnopqrstuvwxyz']; print(alphabet)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> get_available_letters("o", alphabet)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '_', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> get_available_letters("e", alphabet)
['a', 'b', 'c', 'd', '_', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '_', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> get_available_letters("r", alphabet)
['a', 'b', 'c', 'd', '_', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '_', 'p', 'q', '_', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> get_available_letters("z", alphabet)
['a', 'b', 'c', 'd', '_', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '_', 'p', 'q', '_', 's', 't', 'u', 'v', 'w', 'x', 'y', '_']
对于其他函数,您需要在外部声明变量listLength并将其传递给该函数。
def get_guessed_word(secret_word, letters_guessed, listLength):
for i,char in enumerate(secret_word):
if char == letters_guessed:
listLength[i]=char+" " # Permanently modifies listLength, not breaking since multiple same letters can occur in the same word
listCopy = listLength[:]
print(listCopy)
>>> secret_word = "overgrown"
>>> listLength = ["_ "]*len(secret_word); print(listLength)
['_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ']
>>> get_guessed_word(secret_word, "o", listLength)
['o ', '_ ', '_ ', '_ ', '_ ', '_ ', 'o ', '_ ', '_ ']
>>> get_guessed_word(secret_word, "e", listLength)
['o ', '_ ', 'e ', '_ ', '_ ', '_ ', 'o ', '_ ', '_ ']
>>> get_guessed_word(secret_word, "r", listLength)
['o ', '_ ', 'e ', 'r ', '_ ', 'r ', 'o ', '_ ', '_ ']
>>> get_guessed_word(secret_word, "z", listLength)
['o ', '_ ', 'e ', 'r ', '_ ', 'r ', 'o ', '_ ', '_ ']
并在需要修改数组而不影响原始数组时制作数组的副本。