Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/authentication/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python A453任务1“真实循环时的帮助”_Python_Loops - Fatal编程技术网

Python A453任务1“真实循环时的帮助”

Python A453任务1“真实循环时的帮助”,python,loops,Python,Loops,我试图做一个循环,当在句子中找不到单词时,它会要求重新启动程序,而不是结束程序。这是我的密码: UserSen = input("Please type in a sentence without punctuation: ") #This will allow the user to input a sentence UserSen = UserSen.lower() #This will covert the whole sentence to lower case UserSen = s

我试图做一个循环,当在句子中找不到单词时,它会要求重新启动程序,而不是结束程序。这是我的密码:

UserSen = input("Please type in a sentence without punctuation: ") #This will allow the user to input a sentence
UserSen = UserSen.lower()  #This will covert the whole sentence to lower case
UserSen = str.split(UserSen)   #This will return all the words in the UserSen as a list using str. as a separator

Word_To_Find = input("Please enter a word you want to search for in the sentence: ")  #This will allow the user to input a word to find
Word_To_Find = Word_To_Find.lower()  #This will covert the word to be searched into lower case

Position = []  #Variable position is declared so it can be used later in the search part (for loop)
Sentence_length = (len(UserSen))  #Measure length of sentence

while True:
    while True:
        if Word_To_Find not in UserSen: #If the word to search is not in sentence
            print ("Error this word: ",Word_To_Find,"isn`t in the sentence" )#Display error message if word is not in the sentence
            answer = raw_input('Run again? (y/n): ')
             if answer in ('y', 'n'):
                break
            print ("Invalid input")
        if answer == ("y"):
            continue
        elif:
            print ("Goodbye")
else: # But if the word is found starts another iteration
    for i in range(0,Sentence_length): # Range will generate sequences of numbers in the form of a list and select the position withing the sentence and store it in the (i)ndex.
        if UserSen[i] == Word_To_Find: #if the index is equivalent to the word
            Position.append(i+1) # this will fill the empty variable i declared earlier with position(s) as this will add the word to the position list
print ("Search successful. The word",Word_To_Find,"' has been found in position:",Position) # This will display the position of the word if the word is found

有人能帮忙吗?

如果出于某种原因希望重新运行代码,您可以随时再次调用该函数:

def function_containing_what_you_want_to_rerun(necessary_arguments):
    #code you would want to rerun


while condition_for_rerun:
    function_containing_what_you_want_to_rerun(args)
    #Reevaluate condition_for_rerun