It doesn't output anything although it should....I really don't understand how or why. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

It doesn't output anything although it should....I really don't understand how or why.

The following code consists of 3 functions starting form the last one "numssplite()" where it should itrate over a list and find all the numbers inside in case it does find it adds a space before and after the number but if the number consist from more than one number it calls "check()" which checks if the parameter is a number if yes it adds one to the number's index and calls another function "checkagain()" to check if the new item is a number if yes it adds one to the index and calls "check()" in case the item is not a number then they just add a space before that item to separate the numbers from everything else. https://code.sololearn.com/cmYJegm1KK6q/?ref=app

28th Oct 2023, 5:09 PM
Intermediate Depression
Intermediate Depression - avatar
8 Answers
+ 9
Intermediate Depression , (can you confirm that an input of 'a1b23x' should result in: 'a 1 b 23 x') ? there are several issues in the code, but i did not have checked the complete code. > the main issue is that the code is running in an *infinite loop* in the function numsplite(). since the code inserts spaces before a single digit, the list is increasing during the for loop. this causes problems with indexes. > it is not recommended to insert (or to delete) elements during a running loop. > it is better to create a new resulting list. > also the detection of single or multiple digit numbers can be done in a more simpler way by using the regex module. > the second issue is that the line that inserts spaces should be: lst.insert(lst.index(i), " ")
28th Oct 2023, 7:32 PM
Lothar
Lothar - avatar
+ 4
Intermediate Depression , you almost correctly described the logic of the task, but you write: "...if a number consists of more than one number, it calls check()..." How did you implement a two-digit number search? This is not in your code. In fact, you wrote two absolutely identical functions calling each other, thereby creating an infinite loop, (the functions were invented in order not to write the same operations). As a result, these functions create an infinite call to each other. The third function is also infinite because as soon as it finds a number, it shifts it to the next index by putting a space in front of it, as a result, it bumps into the same number again to infinity. So, I think it's easier to write a new code than to fix this one, since there are a lot of mistakes in it. Start writing new code. The first thing you need to do is find a digit in the text, then check if there is another digit after it and only then select the found number from the text. Write the code in parts...
29th Oct 2023, 2:20 AM
Solo
Solo - avatar
+ 3
Lothar and Solo are right, altering a list inside a loop can be confusing because the size of the list can keep changing, and rewrite from a start is a better solution. You can always create new variables as storage of your intermediate product, and combine them to give a final result. It makes your code easier to understand. Also please avoid using python's function name as a variable name. As you can see in the playground "list" is colored differently. Once you use a function name as a variable you lost that function. And finally, it took me a hard time to understand your <one liner> writting style. Pleaes try to write in a reader friendly way with paragraph and punctuation. It helps others to understand you question quickly.
29th Oct 2023, 2:49 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Lothar Wong Hei Ming Solo Thank you guys very much. I really appreciate your answers you have no idea how much you have helped me. I rewrote the code as you suggested, it was actually the 4th time I rewrite this code haha but it was worth it, it was fun (I almost jumped out of the window to be truthful lol.) And decided to show you the code and hear your thoughts on it. https://code.sololearn.com/cXwd2sQbPgV4/?ref=app
29th Oct 2023, 2:43 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Intermediate Depression Doesn't you want the output to be a string? Why not "join" them?
29th Oct 2023, 3:05 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming not at all lol this is just a part from a much bigger project I am not done at all still gonna put it together and stuff haha
29th Oct 2023, 4:52 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Intermediate Depression , well, that's a completely different matter. If this is for a teacher, then you demonstrate a good knowledge of methods here, you know how to use global variables, compose functions...👏👏👏👍😎 Well, if you wrote for yourself, then of course it's better to do without global variables. Good coding! 🖐️😎
29th Oct 2023, 6:17 PM
Solo
Solo - avatar
0
Solo TYSM❤
30th Oct 2023, 2:37 AM
Intermediate Depression
Intermediate Depression - avatar