+ 2
Can someone pls tell me what is wrong with my code. It a solution for the no numeral
a = input("") numerals = { "1" : "one", "2" : "two", "3" : "three", "4" : "four", "5" : "five", "6" : "six", "7": "seven", "8" : "eight", "9" : "nine" } output = "" for i in a: if i.isdigit(): output += a.replace(i,numerals[i]) print(output)
14 Respostas
+ 4
A couple of things:
Firstly, you don't add a character to 'output' if it isn't a number - only if it is.
Secondly, you don't appear to change "0" to "zero" which the task (possibly) asks you to do.
+ 3
Lord Nader Only you can see your solutions to Code Coach challenges, no one else.
+ 3
Lord Nader you can't look at other people's solutions. You can only see your own once you get in there. Probably because that would be cheating.
G B that's not a valid solution to the challenge. Only numbers less than or equal to ten should be converted, which is part of their issue.
Kwasi Ansah you need to first break the input into a list of words and not go over each individual character. Then you can check if each word isdigit and less than 11. Then do your conversion, but yes, as Russ stated, you need to add 0 "zero" and maybe 10 "ten" to your dictionary.
+ 2
if i.isdigit():
output += numerals[i]
else:
output += i
+ 2
Try this code:
msg=input()
num={"1":"one","2":"two","3":"three","4":"four","5":"five","6":"six","7":"seven","8":"eight","9":"nine","10":"ten"}
for i in msg.split():
if(num.get(i,0)):
print(num[i],end=" ")
else:
print(i,end=" ")
+ 1
when input is "123" you will replace "1" by "one" in first iteration. but "23" is still there, so you append "one23" to output...
next will be "1two3", then "12three"...
better way is, to only use one digit of input per iteration..
Try this:
output=""
for idx, val in enumerate(a):
if val.isdigit():
output += a[idx].replace(val,num[val])
print(output)
+ 1
Russ:
Sometimes easy is better :D
+ 1
Kwasi Ansah first try using split() on your input so that 'a' is now a list of string (words) instead of just an input string.
Then make sure you make the changes to your dictionary so that it covers all values 0-10. Then make a change to your loop (there are a few ways you could go here, but I'm going to try and keep it simple) so that you can get both the value and the index of the element in the list 'a'. See the enumerate() method. You can then check if the element in the list is a number and if its value is in the given range. If so use the index of the current element to change the element in the list 'a' using your dictionary. Once you have completed looping the elements you can join() the list back to a string and output it.
+ 1
Well, Russ suggestion is very clear and simple...
You don't have to take pain splitting input and then iterating, which can be done without splitting....
As you have in your example....
0
Go check my solution for it it will help you understand it
0
G B i dont get the meaning of the idx
0
Idx is the index.. it will count up by each iteration...
But forget my answer.. sometimes I think too weird...
Russ has a very nice solution;)
0
Pls can u show me an example
0
Add 0 to the dictionary (I don't learn Python so I think it's not called dictionary)