2015-11-01 45 views
1

我在python中创建了一个程序,它基本上从句子中获取每个单词并将它们放入回文检查程序中。我有一个函数可以删除放入句子的任何标点符号,一个函数可以找到句子中的第一个单词,一个函数可以在句子的第一个单词之后得到剩下的单词,还有一个函数用于检查回文。Python如何阻止我的循环

#sent = input("Please enter a sentence: ")#sent is a variable that allows the user to input anything(preferably a sentence) ignore this 

def punc(sent): 
    sent2 = sent.upper()#sets all of the letters to uppercase 
    sent3=""#sets sent3 as a variable 
    for i in range(0,len(sent2)): 
     if ord(sent2[i])==32 : 
      sent3=sent3+sent2[i] 
     elif ord(sent2[i])>64 and ord(sent2[i])<91: 
      sent3=sent3+sent2[i] 
     else: 
      continue 
    return(sent3) 


def words(sent): 
    #sent=(punc(sent)) 
    location=sent.find(" ") 
    if location==-1: 
     location=len(sent) 
    return(sent[0:location]) 

def wordstrip(sent): 
    #sent=(punc(sent)) 
    location=sent.find(" ") 
    return(sent[location+1:len(sent)]) 

def palindrome(sent): 
    #sent=(words(sent)) 
    word = sent[::-1] 
    if sent==word: 
     return True 
    else: 
     return False 



stringIn="Frank is great!!!!" 
stringIn=punc(stringIn) 
while True: 
    firstWord=words(stringIn) 
    restWords=wordstrip(stringIn) 
    print(palindrome(firstWord)) 
    stringIn=restWords 
    print(restWords) 

现在我正在尝试使用字符串“Frank is great !!!!”但我的问题是,我不知道如何停止循环程序。该程序不断获得字符串的“GREAT”部分,并将其放入回文检查程序中,以此类推。我如何让它停止,所以它只检查一次?

+2

请仔细阅读[如何创建一个最小的,完整的,并且可验证的示例](http://stackoverflow.com/help/mcve)。 – GingerPlusPlus

回答

0

你可以阻止它这样

while True: 
    firstWord=words(stringIn) 
    restWords=wordstrip(stringIn) 
    #if the word to processed is the same as the input word then break 
    if(restWords==stringIn) : break 
    print(palindrome(firstWord)) 
    stringIn=restWords 
    print(restWords)