python - how do i find the word position of a word that appears more than once in a string using a for loop -
loop = true while loop yes no maybe sentence : ", mylist.index(wordchosen) + 1)
sorry had :(
your question confusing because contained code wasn't styled code. should include plain english description of problem. tried guess :)
your code has indentation issues, doesn't run such.
you use input()
function, fails when sentence entered:
write sentence tre sdf fre dfg traceback (most recent call last): file "bin/tst.py", line 14, in <module> sentence = input() file "<string>", line 1 tre sdf fre dfg ^ syntaxerror: invalid syntax
from input()
documentation:
consider using
raw_input()
function general input users.
after switching input()
raw_input()
issue pops up: code doesn't appear work. that's because you're using is
identity operator check equality, won't produce result you're expecting. see: why comparing strings in python using either '==' or 'is' produce different result?
after switching is
==
code works intended it:
write sentence input sentence input sentence choose word sentence find word postion of sentence ('word position sentence : ', 5) write sentence
side note: don't need use paranthesis around variables: (wordchosen)
.
there more efficient/elegant/pythonic ways of checking if wordchosen
in mylist
, see other answers.
Comments
Post a Comment