What is wrong in this code coach solution of "Military Time" | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

What is wrong in this code coach solution of "Military Time"

I don't know why but this "lst" returns None when printed Please help https://code.sololearn.com/c5SZM29vXodo/?ref=app

1st Jan 2023, 7:16 AM
Arin
Arin - avatar
15 Respuestas
+ 3
Alright ... t = tinput.split(" ") Creates a `list` of string lst = t[0].split(':').append(f) t[0].split(':') splits first element in <t> and creates another `list`. This temporary `list` is the calling `list` - it calls its append() method to add <f> in as new item. append() method added <f> into the temporary `list` but append() doesn't return a new `list` with the new item. Since append() doesn't return anything, a `None` object is assigned as data for variable <lst>. If append() return a new `list` with the new item in, then that new `list` can be said as the modified `list`. But no, append() returns nothing ...
1st Jan 2023, 3:49 PM
Ipang
+ 4
Arin the .append(f) is not doing anything useful. Remove it. If I enter 12:30 AM it wrongly prints 12:30. It should print 00:30.
1st Jan 2023, 7:55 AM
Brian
Brian - avatar
+ 3
append() method only appends something to the calling `list`, the method doesn't return the modified `list`. In Python, functions/methods that doesn't explicitly return anything will, by default, returns `None`
1st Jan 2023, 9:30 AM
Ipang
+ 2
Arin I admit that it puzzles me, too. If you do the append on separate line, then it works (lst.append(f)). But in the final analysis it is unnecessary for this program.
1st Jan 2023, 8:02 AM
Brian
Brian - avatar
+ 2
Brian ok then Thanks for help
1st Jan 2023, 8:04 AM
Arin
Arin - avatar
+ 2
Rob the task we are discussing is from Code Coach. https://sololearn.com/coach/70/?ref=app Please check it. Your solution gives an error when using input from the Code Coach tests. I have a three-line solution, but I am not posting it here. Understand that Sololearn discourages posting solutions to Code Coach, as that defeats the purpose of learning by doing it yourself.
2nd Jan 2023, 2:04 PM
Brian
Brian - avatar
+ 2
A simplified version Arin Check it out Please give me upvote https://code.sololearn.com/c0Tf803CATkS/?ref=app
2nd Jan 2023, 10:48 PM
Wiseman Umanah
Wiseman Umanah - avatar
+ 1
Ipang can you please shortly explain what is calling list and what is modified list?
1st Jan 2023, 10:46 AM
Arin
Arin - avatar
+ 1
Ipang ok thanks for help
1st Jan 2023, 4:16 PM
Arin
Arin - avatar
+ 1
Rob your code had some mistakes I fixed it and tried to simplify it Although i don't think doing this in this method is good, u should try to apply something better than this https://code.sololearn.com/cVkUfeAUWFpe/?ref=app
2nd Jan 2023, 3:07 PM
Arin
Arin - avatar
+ 1
Arin I sent the link to you in DM.
2nd Jan 2023, 6:31 PM
Brian
Brian - avatar
+ 1
#just one line without import of corse.. from dateutil import parser print(parser.parse(input()).strftime("%H:%M")) #learn libraries if you want to do something like that..
2nd Jan 2023, 7:00 PM
Smith Welder
Smith Welder - avatar
0
Brian ok I understand my mistake now But can u tell me why is it printing none when I do print(lst)? It should print a list with 3 items
1st Jan 2023, 7:59 AM
Arin
Arin - avatar
0
I just did it really simply. You guys all complicate it. Here is my code if you wanna check it out. And btw try to name your variables so it's easier to read for yourself and other people https://code.sololearn.com/cJudHkq1G4ME/?ref=app
2nd Jan 2023, 12:51 PM
Rob
0
Brian can u send me the three line solution I am eager to see this because i have done it in a long method
2nd Jan 2023, 3:09 PM
Arin
Arin - avatar