I want to write an algorithme which search for thé rank of a charactere in a list on python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I want to write an algorithme which search for thé rank of a charactere in a list on python

I=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' , 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ':', '/',' '] W=1 X=str(input('Wich charactere do you search in I? ')) for A in I: if A!=X: W+=1 elif A==X: return W Print(W) But it don't work Can you help me please?

19th Sep 2019, 9:14 PM
iren yeger
iren yeger - avatar
7 Answers
+ 3
here you go.. for A in I: if A!=X: W+=1 elif A==X: print(W)
20th Sep 2019, 4:01 AM
Choe
Choe - avatar
+ 2
A more simplified code would be: l = [chr(i) for i in range(ord('a'), ord('z')+1)]+[':', '/', ' '] x = input('Which.. : ') for i in range(len(l)): if(l[i]==x): print(i) break
19th Sep 2019, 10:39 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Aymane Boukrouh, there is a syntax error in your code sample: l = [chr(i) for i in range(ord('a'), ord('z')+1))]+[':', '/', ' '] # here is a rounded bracket too much ^ should be: l = [chr(i) for i in range(ord('a'), ord('z')+1)]+[':', '/', ' ']
20th Sep 2019, 6:45 AM
Lothar
Lothar - avatar
+ 2
Lothar ah sorry, I didn't check the code, usually the text editor helps me with that, thanks!
20th Sep 2019, 7:07 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
One line code: print([*[chr(i) for i in range(ord("a"),ord("z")+1)],":","/"," "].index(input("Wich character do you search?\n"))+1)
20th Sep 2019, 7:25 PM
Sousou
Sousou - avatar
+ 2
Thank You all i solved it with tout help
21st Sep 2019, 8:16 AM
iren yeger
iren yeger - avatar
+ 1
Either write in french or english please. You have a syntax error, it's print not Print. Also, applying str on an input is useless because input is by default a string. EDIT: I just saw the return W line, you cannot use return outside a function, you should probably go back and study python functions
19th Sep 2019, 10:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar