Why cant i get the index number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why cant i get the index number?

The user looks for an age, the method should then look for that number in the age section of the list and if it does exist there it should print out the position of that value https://code.sololearn.com/cHSG42NUYWxS/?ref=app

1st Mar 2022, 11:14 PM
Lenoname
12 Answers
+ 3
Lenoname Show your code with my fixes so I can see how you manage to get two results. The function I provided works as per your requests. While not really practical, I would write it differently.
2nd Mar 2022, 1:37 AM
Solo
Solo - avatar
+ 1
You've made too many mistakes: 1. The find_age() function was not called; 2. The required element was not specified in the .index(?) method; 3. Have not created a complete list of ages. def find_age() : f = int(input('Find age: ')) for i in personList : c.append(i.age) #debug if i.age == f : print(c.index(f)) #debug find_age() #debug
2nd Mar 2022, 12:09 AM
Solo
Solo - avatar
0
FF9900 i dont know if has something to do with what u said but it says valueerror and True is not in the list, worked fine on solo but replit once again…
1st Mar 2022, 11:43 PM
Lenoname
0
FF9900 prints out only one position even if there are multiple people with the same age
1st Mar 2022, 11:59 PM
Lenoname
0
FF9900 Would changing this a lil bit have the same result? def find_age() : #f = int(input('Find age: ')) #print([a for a in personList if a.age == f])
2nd Mar 2022, 12:11 AM
Lenoname
0
FF9900 the code u wrote works,i’m just curious about the code with print, if i were to change some things it might have worked also, since it prints out the right values but not their indexes
2nd Mar 2022, 12:19 AM
Lenoname
0
FF9900 i did and tried to print(index(mylist)) but crashed
2nd Mar 2022, 12:50 AM
Lenoname
0
FF9900 f = int(input()) m = [a.age for a in personList if a.age == f] print(m.index(f)) —>outputs 0
2nd Mar 2022, 1:27 AM
Lenoname
0
f = int(input()) m = [a for a in personList if a.age == f] print(m.index(f)) —> made it crash
2nd Mar 2022, 1:29 AM
Lenoname
- 1
Solo i tried your version, it gave me 2 numbers of the same value for example: 0 0, but thats not possible
2nd Mar 2022, 12:44 AM
Lenoname
- 1
No
2nd Mar 2022, 4:42 PM
Solopro
- 1
enumerate()
3rd Mar 2022, 8:38 AM
d.superbugs