Currently working on an assignment and a little stuck. Looking for some assistance to approach this. I'm trying to try a function that takes two values a magazine and a ransom which are input by a user. If the characters in the ransom can be found in the magazine I want to return that it is true otherwise if the ransom string cannot be found in the magazine string return false. ransom is split into a dictionary {key, vaue} so for example the user enters:
Enter magazine: hello there
Enter ransom: hello
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
{'h': 1, 'e': 1, 'l': 1, 'o': 1}
This should return true but it returns false because it does not count the second 'l' in 'hello'. What am I doing wrong?
def compare(magazine, ransom):
matches = {}
for ch in ransom:
if ch in magazine:
if ch in matches:
matches[ch] += 1
else:
matches[ch] = 1
if ransom in matches:
return True
else:
return False
ransom in matcheschecks if a whole word ransom is in keys belonging to the dictionary{'h': 1, 'o': 1, 'l': 2, 'e': 1}.somethin in dictionaryis going to returnTrueonly if you provide some string which is a key in provided dict.