def translate(phrase):
translated_word = phrase
for letter in phrase:
if letter == "a":
translated_word.replace(letter, "1", 1)
print(translated_word)
print(translate(input("Enter: ")))
Output: None
Asked
Active
Viewed 103 times
-3
-
Can you please give an example? – Nir Elbaz Apr 20 '21 at 14:40
-
``translate`` does not return anything – Mike Scotty Apr 20 '21 at 14:41
-
1this question may help: https://stackoverflow.com/questions/41922629/convert-text-to-braille-unicode-in-python/41922947#41922947 – hiro protagonist Apr 20 '21 at 14:41
-
@MikeScotty It doesn't return but print the output. – Bhavyadeep Yadav Apr 20 '21 at 14:45
-
@BhavyadeepYadav yes, I know, but ``print(translate(...))`` results in ``Output: None``. – Mike Scotty Apr 20 '21 at 14:48
-
@MikeScotty Oh! – Bhavyadeep Yadav Apr 20 '21 at 14:48
-
3Does this answer your question? [can you write a str.replace() using dictionary values in Python?](https://stackoverflow.com/questions/14156473/can-you-write-a-str-replace-using-dictionary-values-in-python) – Henry Ecker Apr 21 '21 at 04:10
2 Answers
3
def translate(phrase):
translated_word = phrase
translationDict = {
'a': '1'
}
for k,v in translationDict.items():
translated_word = translated_word.replace(k, v)
return translated_word
print(translate('dupa'))

Vulwsztyn
- 2,140
- 1
- 12
- 20
-
2Good code. The answer would be even better if you added some explanation. – Matthias Apr 20 '21 at 14:46
-
I must get used to explaining my code :), I always think it's self-explanatory – Vulwsztyn Apr 20 '21 at 14:50
1
I’m not sure why you try to write a function to do that. The standard string method replace()
already does what you want:
print(input("Enter: ").replace("a", "1"))
Note that the replace()
method does not change the string (strings are immutable), but instead it returns a new string.

inof
- 465
- 3
- 7