0
What is wrong in this No Numerals code coach solution
So i have tried to make a No Numerals code with string format function like this : str = input() list = str.split() output = "" for x in list : if x.isnumeric(): x = "{int(x)}".format("zero","one","two","three","four","five","six","seven","eight","nine") output += x else : output += x print(output) But it says that there's a key error in int(x) https://code.sololearn.com/cREgPfZdqzt9/?ref=app
15 Respuestas
+ 1
I don't think you can use a variable as a numbered index for .format. idk why exactly though, or if that's broadly true or just a playground quirk ...just that it seems to be failing.
Also, assignment says to include 10, and you haven't handled punctuation (eg the previous 10 in this sentence), and you gotta add spaces back in between the words.
+ 5
This Should be the correct code:
https://code.sololearn.com/c1XpHhYBFdCJ/?ref
=app
+ 4
Arin The format() method was used incorrectly. Instead of providing the arguments as separate strings, you should pass them as positional arguments within the format string. Additionally, the int(x) part was placed within quotes, which made it treated as a literal string instead of evaluating the x variable as an integer.
+ 4
Arin In the given code, "{:s}".format() is a string formatting technique used to convert a value into a string.
The "{:s}" inside the format function is called a format specifier. The :s specifies that the value should be formatted as a string.
+ 4
Arin If you want to know how that works in that code, ask me I'll explain!
0
It says its fine
0
Orin Cook I see
Thanks
I'll find some other way to do this then
0
Alright, found a workaround:
thing = "{"+x+"}"
x = thing.format("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
(Don't actually use "thing," it's a terrible variable name)
Also, forgot to mention a more critical problem here: test that x isn't a larger number before doing that, or else you'll get index out of bounds errors
0
(Oh, and a minor refinement: remove your else clause and just move output+=x outside of the if)
0
Orin Cook thanks
But can you please tell me what does the "+x+" do
Actually I'm a beginner so i don't know much about that
0
Oh lol, that just concatenates it, ie sticks the strings together. Kinda exactly what you'd naively expect adding strings would do.
note that it's
"{"
+ x
+ "}"
0
Vibhor I see
But can you please explain what does the :s do in x = "{:s}".format...
0
Sure, but we're trying to help fix code, not (re)write it from scratch. Plus, I'm learning more this way lol.