+ 3
Tried solving a code coach problem No numerals but failed one of the test case
https://www.sololearn.com/coach/64?ref=app Here is the code word = input() dic = {0:"zero", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"} str = "" for i in range(len(word) - 1): if word[i].isdigit() and word[i+1].isdigit(): if word[i + 1] == 0 and word[i] == 1: str +="ten" # continue i += 1 else: str += str(word[i]) + str(word[i + 1]) #print (word[i]+word[i + 1] i += 1 elif word[i].isdigit(): str += dic[int(word[i])] else: str += word[i] if word[-1].isdigit(): str += dic[int(word[-1])] else: str += word[-1] print(str)
7 Antworten
+ 3
The problem is that you code is not converting "10" to "ten"
In the *if* statement, you are evaluating string with integers.
+ 2
Thank you, Joshua !! Your code made me laugh at what I did wrong. I had trouble with one of the test cases, too, because ten :)
numlist = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
text = str(input())
for i in range(len(numlist)):
for char in text:
if char == numlist[i]:
text = text.replace(numlist[i], words[i])
print(text)
+ 2
Joshua I copied what you have above into code playground and it gave a type error. As Arsenic mentioned above, word[i] and [i+1] are strings, so Python cannot evaluate them with == 0 or 1 because those are integers. After I put quotation marks around the number (‘0’), the error went away. I used “the zoo has 10 zebras” as a test and your code returned “the zoo has tenzero zebras”. This is because after the initial “if” for 10, your code continued ro search for 0s without regard to whether there is 1 in front.
FWIW, I revised my code based on what I learned from you and now it converts both single digits and 10:
if "10" in text:
text = text.replace("10", "ten")
for i in range(len(numlist)):
for char in text:
if char == numlist[i]:
text = text.replace(numlist[i], words[i])
+ 2
Yes! After adding the if “10” in text, it works with all of Sololearn’s test cases.
+ 1
Did it fix your code Isabel
0
#try this code
line=(input())
dict1={
"0":"zero",
"1":"one",
"2":"two",
"3":"three",
"4":"four",
"5":"five",
"6":"six",
"7":"seven",
"8":"eight",
"9":"nine"
}
for i in line:
if "10" in line:
line=line.replace("10","ten")
elif i in dict1:
line = line.replace(i,dict1[i])
print(line)