1

How do I stop this while loop?

Sentence = input("Please enter the sentence: ").lower()

if "." in Sentence or "'" in Sentence or "," in Sentence or ";" in Sentence or ":" in Sentence or "/" in Sentence or "?" in Sentence or "!" in Sentence or "-" in Sentence:  
    while Sentence:
        print("Your sentence is invalid. Please enter the sentence without punctuation") 
else:
    allWords = Sentence.split()

    print(allWords)

    FindWord = input("Enter the word you are looking for: ").lower()
    for L in range(len(allWords)):
        if FindWord == allWords[L]:
            print("The word <", FindWord, "> is found in", L,"th positions")
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • 4
    Please read documentation on How ask a good question : http://stackoverflow.com/help/how-to-ask – Essex Dec 15 '16 at 18:45

3 Answers3

1

Use the break keyword to break out of the loop when your condition is satisfied. Written a bit shorter, you can do something like

while True:
    Sentence = input("Please enter the sentence: ").lower()
    if any(c in Sentence for c in ".',;:/?!-"):
        print("Your sentence is invalid. Please enter the sentence without punctuation")
    else:
        # Accept input
        break
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
1

Set Sentence to falsy value (False, '', 0) if you aren't going to re-use it.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
-2
Sentence=input("Please enter the sentence: ").lower()

while "." in Sentence or "'" in Sentence or "," in Sentence or ";" in Sentence or ":" in Sentence or "/" in Sentence or "?" in Sentence or "!" in Sentence or "-" in Sentence::    
    print("Your sentence is invalid. Please enter the sentence without punctuation") 
    Sentence=input("Please enter the sentence: ").lower()

Use the conditional to check whether to keep looping. and put a way to change Sentence in the loop

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96