+ 2

Why does the translation table fail in this code?

A value error occurs on translation specially saying the length of the keys in the translation table must be 1 https://code.sololearn.com/cf3tUWb32HHH/?ref=app

16th Jan 2023, 12:23 AM
Gemuh Hans
28 odpowiedzi
+ 1
Fxfx right, I missed that part. or if you add a whitespace after each word, you can skip the list join part. strip() cleans up the result. mydict={'0': 'zero','1': 'one','2': 'two','3': 'three','4': 'four','5': 'five','6': 'six','7': 'seven','8': 'eight','9': 'nine','10': 'ten'} txt = input().split() translated="" for i in range(len(txt)): if txt[i] in mydict: translated+=mydict[txt[i]]+" " else: translated+=txt[i]+" " print(translated.strip())
18th Jan 2023, 2:07 AM
Bob_Li
Bob_Li - avatar
+ 2
#try inputting # I have 1 pen and 2 pencils. # or 1 2 3 10 20 101 # be sure to leave space before and after the numbers. It is just simple replacement. You would need to use regex otherwise. edit: i found out the simpler method was iterating the characters, not the word, and 10 was becoming oneten. So the words have to be split first. The code below should work. It could be optimized, but maybe other members would give it a shot. txt = input().lower() mydict={'0': 'zero','1': 'one','2': 'two','3': 'three','4': 'four','5': 'five','6': 'six','7': 'seven','8': 'eight','9': 'nine','10': 'ten'} wlist = txt.split() for i,w in enumerate(wlist): if w in mydict.keys(): wlist[i] = mydict[w] translate = ' '.join(wlist) print(translate)
16th Jan 2023, 6:52 AM
Bob_Li
Bob_Li - avatar
+ 2
You're right. Good night...
18th Jan 2023, 2:31 AM
Fxfx
Fxfx - avatar
+ 1
Yes it's zero. The code works when I pass a single key like 1 but if I pass a phrase like: I have 1 pen and 2 pencils, I can't get this result: I have one pen and two pencils
16th Jan 2023, 6:38 AM
Gemuh Hans
+ 1
I tried already but I still get a key error since the program is expecting only one input and not a string of words input().lower() mydict={'0': 'z','1': 'one','2': 'two','3': 'three','4': 'four','5': 'five','6': 'six','7': 'seven','8': 'eight','9': 'nine','10': 'ten'} #trans_dic = str.maketrans(mydict) #tanslated = txt.translate(trans_dic) #print(tanslated) # the code above doesnt work trans table is not to be used in this case # advised my Bob li to use input As key to output values each time a key is met for key in txt: translated = mydict[txt] print (translated )
16th Jan 2023, 6:46 AM
Gemuh Hans
+ 1
Please can you walk me through your code with a little explanation
16th Jan 2023, 10:03 AM
Gemuh Hans
+ 1
Ok great thank you. So we split and join in the end right??
16th Jan 2023, 11:04 AM
Gemuh Hans
+ 1
Thank you so much
16th Jan 2023, 5:00 PM
Gemuh Hans
+ 1
Hello Gemuh Hans I think I've got a begining of soluce for you... Try the code below: it is good for 0 to 9, but it return "onez" for 10.... For num>10, I think you'll have to operate by txt.split() to format your str in list, before compare it to mydict... translated="" for i in range(len(txt)): if txt[i] in mydict: translated+=mydict[txt[i]] else: translated+=txt[i] print(translated)
17th Jan 2023, 11:00 PM
Fxfx
Fxfx - avatar
+ 1
Hello Bob_Li. I know that... That's why I propose to operate with txt.split() to transform in word list, before processing the translation. The last thing to do is translated.join(" ") to transform the list in a texte with space. You're not far away ;) Good Luck...
18th Jan 2023, 2:00 AM
Fxfx
Fxfx - avatar
+ 1
Gemuh Hans Maybe reformatting html strings? hacky, obscure stuff, mostly. translate only works on single characters. you can map those characters to a different single character, to a different string, or None. That's why you can't use it in this case, because you have "10":"ten". The key have to be single character. here is a nice video of probable use case: https://m.youtube.com/watch?v=0kYctEx_O28
18th Jan 2023, 2:41 PM
Bob_Li
Bob_Li - avatar
+ 1
Ahhh yes you are right. I tried the code eliminating the key value pair of '10':'ten' and it worked correctly. I now get it.. we can use it only in this situation
18th Jan 2023, 3:33 PM
Gemuh Hans
0
Gemuh Hans maketrans is the wrong thing to use for this. It is used for character replacement. Just use the input as the dict key. txt = input().lower() mydict={'0': 'z','1': 'one','2': 'two','3': 'three','4': 'four','5': 'five','6': 'six','7': 'seven','8': 'eight','9': 'nine','10': 'ten'} translated = mydict[txt] print(translated)
16th Jan 2023, 2:21 AM
Bob_Li
Bob_Li - avatar
0
I tried your modification to my code and it doesn't work I don't understand why. Is there something I am missing
16th Jan 2023, 6:28 AM
Gemuh Hans
0
The input is actually a phrase which contains words and numbers and I am supposed to replace only the numbers to make the phrase read with only words
16th Jan 2023, 6:30 AM
Gemuh Hans
0
are you sure about the z in '0':'z'? maybe it's'zero'
16th Jan 2023, 6:31 AM
Bob_Li
Bob_Li - avatar
0
you have to iterate over the words in the sentence.
16th Jan 2023, 6:41 AM
Bob_Li
Bob_Li - avatar
0
ok, here's the list comprehension version txt = input().lower() mydict={'0': 'zero','1': 'one','2': 'two','3': 'three','4': 'four','5': 'five','6': 'six','7': 'seven','8': 'eight','9': 'nine','10': 'ten'} res = ' '.join([mydict[w] if w in mydict.keys() else w for w in txt.split()]) print(res)
16th Jan 2023, 8:46 AM
Bob_Li
Bob_Li - avatar
0
if you directly iterate a string, it will be character by character. txt = "I have 10 pencils" for c in txt: print(c) I h a v ... We don't want this. We want words. so we split the string into a list, along the blank spaces. wlist = txt.split() wlist = ['I', 'have', '10', 'pencils'] we can then iterate this list then replace the words if they are in mydict. I used enumerate to get the index and value of the items in the list for index, value in enumerate(mylist): if value in mydict.keys(): mylist[index] = mydict[value] Then use join to reform the string from the updated list.
16th Jan 2023, 11:01 AM
Bob_Li
Bob_Li - avatar
0
right.
16th Jan 2023, 11:40 AM
Bob_Li
Bob_Li - avatar