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)

Прямо сейчас я пытаюсь использовать строку "Фрэнк великолепен !!!!" но моя проблема в том, что я не уверен, как остановить цикл программы. Программа продолжает получать "ВЕЛИКОЛЕПНУЮ" часть строки и помещает ее в средство проверки палиндрома и так далее. Как мне заставить его остановиться, чтобы он проверял только один раз?


person John Smith    schedule 01.11.2015    source источник
comment
Прочтите Как создать минимальный, полный и проверяемый пример.   -  person GingerPlusPlus    schedule 01.11.2015


Ответы (1)


ты можешь остановить это вот так

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)
person m7mdbadawy    schedule 01.11.2015