How would i go about making a negative for loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How would i go about making a negative for loop?

Like: word = 'chatting' word2 = 'chat: for word2 not in word: word2 += word I want output to be: word2 = 'chatting' I know I am making a lot of error, so can someone point them out and lead me in the right way? Cause when I put not in a for loop it says syntax error.

24th Oct 2017, 3:54 AM
Poet🎭
Poet🎭 - avatar
9 Answers
+ 7
# "word2 not in word" is a test/condition ans should be used in an "if" statement: if word2 not in word: print("word doesn't contains word2") else: print("word contains word2") """ Also "word2 += word" is equivalent (shorthand) of "word2 = word2 + word"... What are you trying to do by that? What are you expecting from a loop? and from a "negative" one? Looping over each characters in a string should be: """ for c in word: print(c) # will successively output c, h, a, t, t, i, n, g # reverse looping require a few more verbosity: for i in range(len(word)-1,-1,-1): print(word[i]) # will successively output g, n, i, t, t, a, h, c
24th Oct 2017, 4:40 AM
visph
visph - avatar
+ 6
So, why not just do: "word2 = word" ?
24th Oct 2017, 6:19 AM
visph
visph - avatar
+ 6
word1 = "chatting" word2 = "" for c in word1: word2 += c # or: for i in range(len(word1)): word2 += word1[i] # some more code if word2 is 'prefilled': word2 = "chat" if word2 == word1[0:len(word2)]: for i in range(len(word2),len(word1): word2 += word1[i] else: # start of word1 and word2 is not the same """ [edit] You will also be adviced to first test if len(word2) is lesser than len(word1) to avoid an index out of range error when using slice notation to verify that both strings start identically ;) """
24th Oct 2017, 6:31 AM
visph
visph - avatar
+ 6
Hm, this is what I was thinking; it may be off target given the discussion. I'm using single-letter words here...spaces just for clarity. excluded=['b', 'd'] wordlist=['a', 'b', 'c', 'd', 'e'] out = [word for word in wordlist if word not in excluded] print(out) Output: ['a', 'c', 'e'] I'll read through the answers when I get a chance to sit down.
24th Oct 2017, 2:41 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
A list comprehension* with a condition can do this, like: [output for x in list if condition ] # x ==, in, not in... Otherwise what comes to mind is Python's ternary form: for all values... x = output1 if condition else output2 # += too I am not immediately reminded of a way to use loops that way; even my list comprehension iterates all known values and has the test in the 'if'. * I've got an actual example but it appears you wanted a hint.
24th Oct 2017, 4:29 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
@Kirk Schafer You can share your example pls
24th Oct 2017, 6:17 AM
Poet🎭
Poet🎭 - avatar
+ 3
Because I want to do it one letter by a time
24th Oct 2017, 6:22 AM
Poet🎭
Poet🎭 - avatar
+ 3
Having all the advantage of the previous discussion (so I'm just finalizing)... Here's a verbose way to "build up" the partial word, one letter at a time until it matches the full word... fullWord = "abcdefgh" partWord = "abcd" startPosition = len(partWord) # 4 endPosition = len(fullWord) # 8 for index in range(startPosition, endPosition): partWord += fullWord[index] print(partWord)
24th Oct 2017, 6:54 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
@visph I am trying to make word2 = word1
24th Oct 2017, 6:16 AM
Poet🎭
Poet🎭 - avatar