No numerals(python) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

No numerals(python)

This is my code: nums_dict= {"1": "one","2": "two","3": "three","4": "four","5": "five","6": "six","7": "seven","8": "eight","9": "nine", "10": "ten"} phrase = input() x = phrase[0] if x in nums_dict: y = nums_dict[x] print(phrase.replace(x,y)) else: print(phrase) For some reason, I can only get the first 2 test to pass the rest fail. Can someone give me some tips of what I’m doing wrong?

23rd Feb 2023, 3:07 PM
Jacob Murphy
Jacob Murphy - avatar
6 ответов
+ 5
This would be a way to do it with less code. It is a little bit sneaky by modifying the loop iterator variable. This is done in order to eliminate the else clause and keep it down to only one print statement. phrase=input() list=phrase.split(' ') for i in list: if i in nums: i = nums[i] print(i+' ', end='') Edit: Furthermore, you can use the dict get() method to return a default if the key is not found: phrase=input() list=phrase.split(' ') for i in list: print(nums.get(i, i)+' ', end='') Continuing... list is a temporary variable that is not really used. (And for that matter, so is phrase, but I'll keep it for readability). phrase=input() for i in phrase.split() print(nums.get(i, i)+' ', end='')
24th Feb 2023, 1:32 AM
Brian
Brian - avatar
+ 11
Jacob Murphy , your code just takes the first character of your input, searches for it, and replace it if found. then the code is done. hint: [edited] > we have to check every *word* in the input, if it could be found in the dict. > to accomplish this, we have to split the input string at word boundary, which is a space. doing so, python generates a list of individual words > now we can iterate over this list and search if words are in the dict > to replace the numbers by words we can use the dict, no need to use replace
23rd Feb 2023, 5:08 PM
Lothar
Lothar - avatar
+ 8
Jacob Murphy a bit late, but the attached code shows how you can use *replace()* in your code. (it is not the preferred way, but it works) it also demonstrates how to *condens* the code by keeping a good readability: https://code.sololearn.com/czIAKqwvfV8I/?ref=app
24th Feb 2023, 6:06 PM
Lothar
Lothar - avatar
+ 5
Jacob Murphy try a few tests in the Code Playground and see the results. It fails on these, for instance: the 3 pied pipers played 10 chickens crossed the road
23rd Feb 2023, 3:26 PM
Brian
Brian - avatar
+ 2
Thanks guys for the imput. Nice to see the communtiy being active. phrase=input() list=phrase.split(' ') word=[] for i in list: if i in nums: x = nums[i] word.append(x) else: word.append(i) result = " ".join(word) print(result) This is the new code. It passes all of the tests. But i was wondering if there was a more condensed way of writing it? Instead of using append, I was trying to write print(str(list).replace(x,i)) But didnt work because the output would be in str. How could i change it to make it worl?
24th Feb 2023, 1:22 AM
Jacob Murphy
Jacob Murphy - avatar
+ 2
Jacob Murphy you asked about a more condensed way of writing your code and I would recommend list comprehensions. https://code.sololearn.com/cIQu9c3GD0x0/?ref=app
24th Feb 2023, 9:43 AM
Chuks AJ
Chuks AJ - avatar