Skip to main content
 首页 » 编程设计

Python 代码之不确定文本文件

2025年05月04日24jackei

我的 python 代码需要帮助。我一直在尝试将输入的句子保存到文本文件中,而不重复文件中的单词。我不确定该怎么做。

感谢任何帮助。

这是我的代码:

import sys 
 
#user-friendly, informs the user what do to 
answer = input("What is your name?\n") 
print("Hello " + answer + " and welcome to this program!\n") 
print("This program will ask you for a sentence and print out the positions of the words instead of the actual words. it will then save it in a file with the sentence.\n") 
 
repeat = True 
loop = True 
true = True 
 
#Allows the user to decide whether or not they want to use the program 
while repeat: 
    answer2 = input("Do you want to do run this program?\n") 
    #if the answer is 'no' then the program stops 
    if answer2.lower() == "No" or answer2.lower() == "nah" or answer2.lower() == "no" or answer2.lower() == "n": 
        print ("Okay then ... Bye.") 
        sys.exit() 
    #if the answer is 'yes' then the code continues 
    elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes" or answer2.lower() == "y": 
        print ("Okay then ... \n") 
        while true: 
            if loop == True: 
                sentence = input("Please enter a sentence:\n").lower() 
            #converts the sentence into a list 
            s = sentence.split() 
            #works out the positions of the words 
            positions = [s.index(x)+1 for x in s] 
            print(positions) 
 
            #opens a text file 
            fi = open("CA Task 2.txt", "w") 
            #Allows you to write over  the original content in the file 
            fi.write(str(s)) 
            #it closes the file once you've finished with it 
            fi.close() 
 
            #opens a text file 
            fi = open("CA Task 2.txt", "a") 
            #Allows you to add to the text file instead of writing over it 
            fi.write("\n") 
            fi.write(str(positions)) 
            #it closes the file once you've finished with it 
            fi.close() 
            sys.exit() 
 
    #if the answer is neither 'yes' nor 'no' then the programs jumps to this part and allows the user to try again 
    else: 
        print("Please enter a valid answer! Try again!\n") 

假设输入的句子是“问的不是你的国家能为你做什么,而是你能为你的国家做什么”。

它应该说: 不要问你的国家能为你做什么,而要问你能为你的国家做什么

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 9, 6, 7, 8, 4, 5]

这是可行的,然后必须将其保存到文本文件中: ['问', '不', '什么', '你的', '国家', '可以', '做', '为', '你', '但是', '什么', '你', '可以'、'做'、'为'、'你的'、'国家']

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 9, 6, 7, 8, 4, 5]

这很好,但我希望它做的是,如果该词已在文本文件中被提及一次,则不再重复该词。

请您参考如下方法:

内置函数调用:set https://docs.python.org/3/library/stdtypes.html#set :

import sys 
 
 #user-friendly, informs the user what do to 
 answer = input("What is your name?\n") 
 print("Hello " + answer + " and welcome to this program!\n") 
 print("This program will ask you for a sentence and print out the positions of the words instead of the actual words. it will then save it in a file with the sentence.\n") 
 
repeat = True 
loop = True 
true = True 
 
 #Allows the user to decide whether or not they want to use the program 
 while repeat: 
  answer2 = input("Do you want to do run this program?\n") 
  #if the answer is 'no' then the program stops 
  if answer2.lower() == "No" or answer2.lower() == "nah" or answer2.lower() ==    "no" or answer2.lower() == "n": 
    print ("Okay then ... Bye.") 
    sys.exit() 
#if the answer is 'yes' then the code continues 
elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes" or answer2.lower() == "y": 
    print ("Okay then ... \n") 
    while true: 
        if loop == True: 
            sentence = input("Please enter a sentence:\n").lower() 
        # converts the sentence into a list 
        s = sentence.split() 
        # for loop makes sure that if the word is in the list then it wont print it out again 
        for word in s: 
            if word not in s: 
                s.append(word) 
        # works out the positions of the words 
        positions = [s.index(x) + 1 for x in s] 
        print(set(positions)) 
 
        # opens a text file 
        fi = open("CA Task 2.txt", "w") 
        # Allows you to write over  the original content in the file 
        fi.write(str(set(s))) 
        # it closes the file once you've finished with it 
        fi.close() 
 
        # opens a text file 
        fi = open("CA Task 2.txt", "a") 
        # Allows you to add to the text file instead of writing over it 
        fi.write("\n") 
        fi.write(str(set(positions))) 
        # it closes the file once you've finished with it 
        fi.close() 
        sys.exit() 
 
        #if the answer is neither 'yes' nor 'no' then the programs jumps to this part and allows the user to try again 
        else: 
         print("Please enter a valid answer! Try again!\n")'