2016-12-15 9 views
1

これをどのように停止するのですか?whileループ?どのように私はこれをループ中にループを停止するのですか

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") 
+4

よくある質問をしてください。http://stackoverflow.com/help/how-to-ask – Deadpool

答えて

-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() 

ループを維持するかどうかを確認するために、条件を使用してください。ループ内でSentenceを変更する方法

1

条件が満たされたときにループを解除するには、breakキーワードを使用します。あなたはそれを再使用しない場合は少し短い書かれた、あなたはfalsy値(False, '', 0)

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 
1

設定Sentenceような何かを行うことができます。

関連する問題