Adam Spencer Baker
Adam Spencer Baker

Reputation: 53

how to store dictionary items in a list

This question is sorta hard to make into a one lined question so im going to have to explain. Im trying to check a given string to see if the contents in the string has whats inside the dictionary, if that situation is true for every letter in the dictionary store the keys in the list "Notes". But how do you store it in the list and how do I return it because I keep getting nothing in return heres what i have:

def text2notes (s):
    s = s.lower()
    noteBook = { "e":30, "t":31, "a":32, "o": 33, "i": 34, "n" :35, "h": 36}
    Notes = []
    for NoteBook in s:
        if s in noteBook:
            Notes.append(noteBook[s])
    return Notes

please help.

Upvotes: 0

Views: 220

Answers (3)

Chris Homere
Chris Homere

Reputation: 1

You could convert your check into a Boolean and then make a conditional statement :

    def text2notes(s):
      s = s.lower()
      noteBook = {"e": 30, "t": 31, "a": 32, "o": 33, "i": 34, "n": 35, "h": 36}
      Notes = []
      Note = (notebook in s)
      print Note
      if Note is True:
         Notes.append(noteBook[NoteBook])

      return Notes

Hope this helps

Upvotes: -1

Abdelrahman Elkady
Abdelrahman Elkady

Reputation: 2576

Check this :

def text2notes(s):
  s = s.lower()
  noteBook = {"e": 30, "t": 31, "a": 32, "o": 33, "i": 34, "n": 35, "h": 36}
  Notes = []
  for NoteBook in s:
    print NoteBook
    if NoteBook in noteBook:
      Notes.append(noteBook[NoteBook])

  return Notes

Checking for the NoteBok not s in the loop

Upvotes: 0

kennes
kennes

Reputation: 2145

Try this instead:

for character in s:
    if character in noteBook:
        Notes.append(noteBook[character])

Also, make sure the return Notes is outside of the for loop.

Upvotes: 3

Related Questions