for loop using dictionary but only works if the key is a single character

input() returns a string. When you iterate over a string, it iterates over single characters. I.e.

my_str = "abcdef"
for e in my_str:
    print(e)

Output:

a
b
c
d
e
f

What you really want is to iterate over words, which can be done by splitting the string on whitespaces. So you can do this via .split() :

words_to_translate = input("Please type the words to translate ").split() # splits on whitespace by default (i.e. space/tabs)

for word in words_to_translate:
    # rest of the code

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top