Home > Software design >  how to fix printing memory address in python
how to fix printing memory address in python

Time:04-15

I've been trying to fix this problem with my code, where the memory address is printed in the console. I suspect it's something to do with iterating through the "wordlist" list.

#wordlist is actually around 500 words long.
wordlist = ['games', 'happy', 'gxems', 'hpapy']

def get_possible_letters(word):
  letters = "abcdefghijklmnopqrstuvwxyz"
  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) 

CodePudding user response:

The last statement should be

print(get_most_information())

instead of

print(get_most_information)

The latter merely prints the location of the function in memory instead of printing the function's output.

CodePudding user response:

You need to have () on functions to call the function:

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

I've commented some recommended edits below:

# 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
  • Related