Iterating a string and replacing an element in python -
i attempting search through 2 strings looking matching elements. if strings have 2 elements in mutual in different positions, want create element in 'guess' string cow. if strings have 2 elements in same position, element bull.
here have:
if index(number,i) in guess , not index(guess,i) == index(guess,i): replace(index(guess,i),'cow') if index(guess,i) == index(number,i): replace(index(guess,i),'bull')
i'm not sure if i'm using index correctly.
first off, need using index()
, replace()
string methods, martijn said in comment.
this so: guess.index(i)
find index of i
in string guess
.
you might want check out find() same index()
won't raise exception when substring not found.
also note seeing if result of index()
in string guess
. error, since integer cannot in string! index()
returns integer!
then consider stating ... , not guess.index(i) == guess.index(i):
(i fixed index
code) makes no sense, since of course of study equal! same thing!
lastly, using replace
incorrectly. the documentation, replace
takes string first argument - not index! seek using so: guess = guess.replace(i, 'bull')
. alter guess
have all occurrences of i
replaced string 'bull'
.
i wasn't concerned actual algorithm here, basic errors.
python string replace indexing
No comments:
Post a Comment