Reputation: 33
I need help with my python code. I've been trying to save the sentence inputted into a text file without it repeating the words in the file. I'm not sure how to do it.
Any help is appreciated.
This is my code:
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")
Let's just say the sentence inputted is "ask not what your country can do for you but what you can do for your country".
It should come up saying: ask not what your country can do for you but what you can do for your country
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 9, 6, 7, 8, 4, 5]
This works and then it has to be saved into a text file: ['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you', 'but', 'what', 'you', 'can', 'do', 'for', 'your', 'country']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 3, 9, 6, 7, 8, 4, 5]
This is fine but what i want it to do is to not repeat the word if it has already been mentioned once in the text file.
Upvotes: 1
Views: 118
Reputation: 925
This:
for word in s:
if word not in s:
s.append(word)
doesn't make sense to me. Are you trying to create a list of unique words? It gives you the same list.
Also if answer2.lower() == "No"
is superfluos, as the result would never be 'No'.
Let's say, you have a list from a sentence where some words are unique, and some are not:
s = ['foo', 'bar', 'foo', 'foo', 'spam']
and you want to get the digital representation of these unique words, you can get it like that:
d = []
i = 0
for item in s:
if item not in s[:i]:
d.append(i)
i += 1
else:
d.append(s.index(item))
Now you get a list where each digit is a unique representation of words in s:
[0, 1, 0, 0, 2]
Upvotes: 0
Reputation: 1779
There is built is function called: 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")'
Upvotes: 3
Reputation: 1957
So it's your for loop section that's not behaving? Looks to me as you are splitting a sentence into a list so ['here', 'is', 'my', 'sentence', 'input']
and then looping through each of those words and appending them back to the list if they aren't already in it. So this should never have any effect on s
.
Python has a set
collection which holds unique values. So it's like a list
but doesn't let you add duplicates. You could use this instead of your for loop as you can initialise a set
with a list
- like the one use are creating from your split()
call.
s = sentence.split()
s = set(s)
Edit: sets do not preserve order like a list
. So if saving the words in order of first appearance is important then this method won't work.
Upvotes: 2
Reputation: 1129
Change the part where you check if the word is in s. You should save your words in another list and check if the s word is already in the other list. Like in the code below:
#for loop makes sure that if the word is in the list then it wont print it out again
new_s = []
for word in s:
if word not in new_s:
new_s.append(word)
Upvotes: 0