+ 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?
7 ответов
+ 3
here you go..
for A in I:
if A!=X:
W+=1
elif A==X:
print(W)
+ 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
+ 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)]+[':', '/', ' ']
+ 2
Lothar ah sorry, I didn't check the code, usually the text editor helps me with that, thanks!
+ 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)
+ 2
Thank You all i solved it with tout help
+ 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