0

why does this loop return NONE as well as the expected outcome

I have tried every thing i know which i admit is not as much as most of you but it will not exit the function correctly https://code.sololearn.com/cV8bCc3zQKEU

11th Mar 2019, 5:32 PM
Michael Harrall
Michael Harrall - avatar
11 Antworten
+ 5
hi Michael, very small issue leads to trouble. from the function you returned ‘true‘ but has to be ‘True’. I have taken your code and updated it so it runs properly. https://code.sololearn.com/cT5GDIughB55/?ref=app
11th Mar 2019, 5:57 PM
Lothar
Lothar - avatar
+ 2
Note that list indices are zero based whereas len() returns the length of the list starting at 1. A list with one element has the length 1, its only element has the index 0. So you should change the first line in the function to if len(input_list)-1 >= index: Also, return input_list.count(index) won't return the number of elements in the list with the same value as the element at index "index", but it will return the number of elements that equal "index" ==> change it to return input_list.count(input_list[index])
11th Mar 2019, 6:22 PM
Anna
Anna - avatar
+ 1
Hello Michael Harrall, according to the comments, you should return the count or empty string, so I believe the function should be like this: def item_count_from_index( input_list, index): if len(input_list) >= index: return input_list.count(index) else: return ""
11th Mar 2019, 5:52 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Thank you I have not codes in so many years I am stumbling on what was and now is
11th Mar 2019, 5:53 PM
Michael Harrall
Michael Harrall - avatar
0
Your code doesn't do what you say, so it's hard to give you an answer.
11th Mar 2019, 5:53 PM
HonFu
HonFu - avatar
0
What were you doing back then?
11th Mar 2019, 5:54 PM
HonFu
HonFu - avatar
0
I have been building the infrastructure that you are using for the last 20 years. I write a lot of bash code. however that is different than python and i need python for what i am going to be working on. I am sure you dont ever have to learn new skills I am very thankfull for the help i get on here and it does run as expected with the Help from the other Gent not just criticism.
11th Mar 2019, 5:58 PM
Michael Harrall
Michael Harrall - avatar
0
Michael Harrall, if you post a question about why your loop returns None and link a code that does something completely different and doesn't contain a loop, I believe it's very reasonable to ask you what's up about that.
11th Mar 2019, 6:05 PM
HonFu
HonFu - avatar
0
HonFu did you actually run the code it does exactly what it should.
11th Mar 2019, 6:10 PM
Michael Harrall
Michael Harrall - avatar
0
Lothar and Ulisses Cruz thank you guys for your great assist
11th Mar 2019, 6:12 PM
Michael Harrall
Michael Harrall - avatar
0
Anna thank you I want to count from 1 and test the index is greater or equal to the count from 1. Then I want to count the number of times that index occurred.
11th Mar 2019, 6:31 PM
Michael Harrall
Michael Harrall - avatar