繁体   English   中英

我如何根据用户在 python 上的回答重新运行程序(是或否)?

[英]How do i re run a program based on answer from the user on python (yes or no)?

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

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
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
run_again = 'yes'

while run_again == 'yes':
    # your original program here

    run_again = ''
    while run_again not in ('yes', 'no'):
        run_again = input('run again (yes/no)?')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM