如何在python中修复打印内存地址



我一直在尝试用我的代码解决这个问题,在控制台中打印内存地址。我怀疑这与遍历"wordlist"列表有关。

#wordlist is actually around 500 words long.
wordlist = ['games', 'happy', 'gxems', 'hpapy']
letters = "abcdefghijklmnopqrstuvwxyz"
def get_possible_letters(word):
  possible_letters = letters
  count = 0
  for i in word:
    if i in letters:
      possible_letters.remove(i)
  return possible_letters
def get_most_information():
  score = 0
  top_score = 27
  for i in wordlist:
    score = len(letters) - len(get_possible_letters(i))
    if score < top_score:
      top_score = score
  return top_score
print(get_most_information) 

最后一条语句应为

print(get_most_information())

不是

print(get_most_information)

后者只打印函数在内存中的位置,而不打印函数的输出。

你需要在函数上有()来调用函数:

# Function must have () on end
print(get_most_information())

我在下面评论了一些推荐的编辑:

# wordlist is actually around 500 words long.
wordlist = ['games', 'happy', 'gxems', 'hpapy']
# Letters must be global, or passed as arg to be used in another function
letters = "abcdefghijklmnopqrstuvwxyz"

def get_possible_letters(word):
    possible_letters = letters
    count = 0
    for i in word:
        if i in letters:
            # Replace, not remove on strings
            possible_letters.replace(i, "")
    return possible_letters

def get_most_information():
    score = 0
    top_score = 27
    for i in wordlist:
        score = len(letters) - len(get_possible_letters(i))
        if score < top_score:
            top_score = score
    return top_score

# Function must have () on end
print(get_most_information())
# Outputs 0

最新更新