简体   繁体   English

子手游戏,但我在使用列表功能时遇到麻烦

[英]Hangman game but I'm having troubles with a list function

I'm currently doing the MIT Opencourseware on Python and one of the assignments is to do a Hangman game. 我目前正在使用python开发MIT Opencourseware,其中一项任务是制作Hangman游戏。
Most of the functions I've managed to do pretty well but the problem I'm encountering is in these two functions: 我设法完成的大多数功能都做得很好,但是我遇到的问题在于以下两个功能:

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

The problem is that each time I go through the function the alphabet resets and I tried solving this by creating a copy but I realized the solution really doesn't work even before implementing it because the listCopy and alphabetCopy just copy the "zero-state" each time the function is called. 问题是,每次我通过该函数时,字母都会重置,并尝试通过创建副本来解决此问题,但我意识到该解决方案甚至在实现之前都无法使用,因为listCopy和AlphabetCopy只是复制“零状态”每次调用该函数。

I know I can do other solutions but I specifically want this "user-experience". 我知道我可以做其他解决方案,但是我特别想要这种“用户体验”。 I tried some other workarounds but I just can't figure it out right now. 我尝试了其他解决方法,但现在无法解决。

get_available_letters get_available_letters

So you have a list with letters that were already guessed and you want to know, what letters are left. 因此,您有一个列表,其中包含已经被猜出的字母,并且您想知道剩下的字母。 The simplest way is to use sets . 最简单的方法是使用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))

What this does: 这是做什么的:

>>>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']

get_guessed_word get_guessed_word

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'

I am assuming that the variable letters_guessed is a list or set containing all letters which have been guessed. 我假设变量letters_guessed是一个包含所有已猜出字母的列表或集合。

In that case, you could use: 在这种情况下,您可以使用:

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', '_']

The '==' operator returns true if the objects being compared have the same value. 如果要比较的对象具有相同的值,则'=='运算符将返回true。 It will return false since one is a string and the other a list, hence can't have same value. 因为一个是字符串,另一个是列表,它将返回false,因此不能具有相同的值。

The 'in' operator returns true if the input on the left is present in the input in the right. 如果左侧输入出现在右侧输入中,则“ in”运算符将返回true。

However 然而

If you are guessing one letter at a time, then you need your functions to leave permanent modifications on the variables outside. 如果您一次猜一个字母,则需要使用函数对外部变量进行永久修改。 So variable alphabet can't be declared inside get_available_letters, but must be declared in the main code and passed as an input to the function get_available_letters. 因此,可变字母不能在get_available_letters中声明,而必须在主代码中声明,并作为输入传递给get_available_letters函数。 This should fix this function if letters_guessed is a string 1 letter long. 如果letters_guessed是一个长度为1个字母的字符串,则应该可以修复此功能。 Now you can use the '==' operator. 现在,您可以使用'=='运算符。

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', '_']

For the other function you would need variable listLength to be declared outside and passed to this function. 对于另一个函数,您需要在外部声明变量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 ', '_ ', '_ ']

And make copies of arrays when you need to modify arrays without affecting the original. 需要在不影响原始阵列的情况下修改阵列并复制阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM