hackintosjdn hackintosjdn - 4 months ago 22
Python Question

CanWeMakeIt function python, it doesn´t return anything


  1. So, I have to write a function that should return the bool value True if you can spell out myWord using only the letters in myLetters, and return the bool value False if it cannot.
    To do this I need my letterPoints dictionary which is:

    global letterPoints
    letterPoints = {"A": 1, "B": 3, "C": 3, "D": 2, "E": 1, "F": 4, "G": 2, "H": 4, "I": 1, "J":8, "K": 5, "L": 1, "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10}




    def canWeMakeIt(myWord, myLetters):
    canMake = True
    letterPoints= list(myLetters)
    length=len(myWord)

    for i in range(length):
    i=i+1
    letter=myWord[i]
    if letter not in letterPoints:
    canMake = False
    else:
    letterPoints.remove(letter)
    return canMake


Answer

Here is your function

def canWeMakeIt(my_word, letters):
   allowed = list(letters):
   for i in my_word:
       if i not in allowed:
           return False
    return True

#NOTICE HOW YOU MUST CALL IT TO DO SOMETHING
print(canWeMakeIt("cat","tack"))

Now let's analize your code :

  • Your indentation is not correct
  • You don't have to increase i in for loop
  • You don't need to use dict if you don't know how, and it's unnecessary

Basicly it's work like this:

def canWeMakeIt(myWord, myLetters):
    canMake = True
    letterPoints = list(myLetters)
    length = len(myWord)
    for i in range(length):
        letter = myWord[i]
        if letter not in letterPoints:
            canMake = False
    return canMake

print(canWeMakeIt("cat","tac"))

You notice the difference ;)

Comments