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

15th May 2023, 1:44 AM
Arin
Arin - avatar
15 Antworten
+ 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.
15th May 2023, 3:56 AM
Orin Cook
Orin Cook - avatar
+ 5
This Should be the correct code: https://code.sololearn.com/c1XpHhYBFdCJ/?ref =app
15th May 2023, 9:15 AM
Vibhor
Vibhor - avatar
+ 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.
15th May 2023, 9:13 AM
Vibhor
Vibhor - avatar
+ 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.
15th May 2023, 9:28 AM
Vibhor
Vibhor - avatar
+ 4
Arin If you want to know how that works in that code, ask me I'll explain!
15th May 2023, 9:29 AM
Vibhor
Vibhor - avatar
+ 1
Arin , Orin Cook , Vibhor For some reason you all are stuck with the format() function, whereas it is not needed at all. You just take the dictionary as in the @Vibhior's answer, and take x-th element of that. ``` nums = dict(enumerate(("zero", "one", "two",... ))) output += nums[int(x)] ```
15th May 2023, 10:48 PM
Евгений
Евгений - avatar
+ 1
Arin the ":s" format specifier is not needed, because it is the default. https://docs.python.org/3/library/string.html#format-specification-mini-language
15th May 2023, 11:00 PM
Евгений
Евгений - avatar
0
It says its fine
15th May 2023, 1:50 AM
Finnlyn Cessna
0
Orin Cook I see Thanks I'll find some other way to do this then
15th May 2023, 4:00 AM
Arin
Arin - avatar
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
15th May 2023, 4:13 AM
Orin Cook
Orin Cook - avatar
0
(Oh, and a minor refinement: remove your else clause and just move output+=x outside of the if)
15th May 2023, 4:16 AM
Orin Cook
Orin Cook - avatar
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
15th May 2023, 4:26 AM
Arin
Arin - avatar
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 + "}"
15th May 2023, 4:41 AM
Orin Cook
Orin Cook - avatar
0
Vibhor I see But can you please explain what does the :s do in x = "{:s}".format...
15th May 2023, 9:21 AM
Arin
Arin - avatar
0
Sure, but we're trying to help fix code, not (re)write it from scratch. Plus, I'm learning more this way lol.
15th May 2023, 11:35 PM
Orin Cook
Orin Cook - avatar