-3

Currently I have my data in a directory

myDict = {'a':'Andy','b':'Bill','c':'Carly' }

I want to achieve something like

input = a --> output = Andy

input = ab --> output = Andy Bill

input = abc --> output = Andy Bill Carly

How can I do that ? Please help me

Eric
  • 2,636
  • 21
  • 25
DAA
  • 1
  • 2

4 Answers4

1
  • Iterate through the individual letters of the input string.
  • Look up each item in the dictionary.
  • Concatenate the results as you go.

Can you handle each of those capabilities? If not, please post your best attempt and a description of the problem.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Thank you for comment. I want you to give this output: isim giriniz: ab çıktı: Ankara Bursa Or; isim giriniz: ac çıktı: Ankara Ceyhan – DAA Oct 25 '17 at 23:55
1

isim is not a function, and all you need to do is access that key in the dictionary

def girdi_al():
    isim = input("isim giriniz: ")
    return isim

def isim_donustur(isim):
    cikti = isim

    donusum = {'a':'Ankara','b':'Bursa','c':'Ceyhan'}

    #How can I do?
    result = []
    for letter in cikti:
        result.append(donusum[cikti])
    print(' '.join(result))


def main():
    isim_donustur(girdi_al)

# === main ===
main() 
yash
  • 1,357
  • 2
  • 23
  • 34
  • I think you misunderstood the question, what if `isim` is equal to `'abc'`? Your code doesn't handle multiple keys. – user_ Oct 25 '17 at 23:50
  • @GBlomqvist oh that was embarassing. Thank you for pointing it out. Modified the code now. – yash Oct 25 '17 at 23:53
1
def girdi_al():
    isim = input("isim giriniz: ")
    return isim

def isim_donustur(isim):
    cikti = isim()

    donusum = {'a':'Ankara','b':'Bursa','c':'Ceyhan'}

    string = ''

    for char in cikti:
        string += donusum[char] + ' '

    print(string )

def main():
    isim_donustur(girdi_al)

# === main ===
main() 
Abend
  • 589
  • 4
  • 14
0

somthing like this

myDict = {'a':'Andy','b':'Bill','c':'Carly' }
input_str = "abc"

" ".join([v for k,v in myDict.items() if k in list(input_str)])
Serg Anuke
  • 168
  • 2
  • 11