A linguist is doing a study on grammatically-correct sentences. Volunteers are invited to complete an activity deciding if a sentence is grammatically correct or not.
Here is an attempt at writing the program.
Type the above code into your IDE.
Choose at least 5 blocks of code and include a comment which explains what the line or block does.
A function needs to be designed:
This function opens a text file called correct.txt and writes s to it on a new line.
This means it appends the sentence onto the end of the file (do some research on file permissions in Python if necessary.
Code this function!
You may need to review how to read a file for this adaptation.
Adapt your program so that the saveToFile function will call another function alreadySaved, passing it the sentence as a parameter.
alreadySaved will return true if the sentence has already been saved, and false otherwise.
So, saveToFile will only save the sentence in the textfile if the call to alreadySaved returns false.
Here is some sample code for the new savedToFile function:
def saveToFile(correctSentence):
result = alreadySaved(correctSentence)
if !result:
#code to save sentence to the file
else:
print("This sentence has already been saved")
One tip is to make a list of things that your program needs to do before you start coding - you could do this as comments in Python eg
#create an empty array
#create a loop to do something 5 times
#ask the user to input a number in the loop
#append the number to the array in the loop
#calculate statistics in the loop
#display all data and statistics after the loop
Then... start coding!