Question

In: Computer Science

This is all in Python. Also, the names come from a .txt file that i have...

This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight).

Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list.

Read Function. The read function should return the contents of the text document as a list.

EXAMPLE OUTPUT
Please enter a name: Jim
Jimis found in the list.
Enter another name (Y/N): Y
Please enter a name: Andy
Sorry, Andy was not found in the list. Would you like to add it? (Y/N): Y
Andrew has been added to the list.
Enter another name (Y/N): N

Write Function. The write function should take the list and override the file with the new names list in reverse alphabetical order with a hard return after each name.

Solutions

Expert Solution

CODE:

#read function
def read():
file = open('names.txt','r')
#reading all the lines and removing the char '\n' from the
#end of each line
lines = [x.split('\n')[0] for x in file.readlines()]
file.close()
return lines
#write function
def write(lines):
#sotring the list in reverse
lines = sorted(lines,reverse = True)
#opening the file
file = open('names.txt','w')
#writing to the file after hard returning after each name
for i in lines:
file.write(i+'\n')
file.close()
  
def main():
#reading the file by calling the read function
lines = read()
choice = 'Y'
#while the user wants to enter a new name
while(choice.lower() == 'y'):
#user enters a name
name = input('Please enter a name: ')
if(name in lines):
#if the name is found in list (case-sensitive search)
print('{} found in list:'.format(name))
else:
#if it is not found in the list
print('Sorry, {} was not found in the list. Would you like to add it?'.format(name))
add = input('(Y/N): ')
#if the user wants to add it to the list
if(add.lower() == 'y'):
#the name is added
lines.append(name)
print('{} has been added to the list'.format(name))
#asking the user to enter another name or not
choice = input('Enter another name (Y/N): ')
#when the user quits the file is updated with a new list
write(lines)
#calling the main function
main()

___________________________________________

CODE IMAGES AND OUTPUT:

________________________________________________

names.txt before executing the program

names.txt after executing the program:

_______________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
A class Names reads the following names from the .txt file Name.txt Jason Butler Paul Cunningham...
A class Names reads the following names from the .txt file Name.txt Jason Butler Paul Cunningham Logan Bryant Cody paul Robert Vernon Ehlers III Quincy James Ellefson Currionte Evans Gavin Scott French Zach Tyler Goss Noah Cosby Jeremy Whittle Ryan brown Jonah Dalton Null Cameron jones Xavier Rhodes Malcom Wesley Jamarcus johnson Bernard smith Joseph Nettles Danny Willaims James wellington William bolar Make a search directory using dynamic allocation where the user would search the any number of letters of...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’...
Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’ winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note that the World Series was not played in 1904 or 1994. There are no entries in the file indicating this.) Write a program that reads this file...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
I have multiple .txt files in a folder, and i want to combine all their contents...
I have multiple .txt files in a folder, and i want to combine all their contents into a single .txt file How do I go about such an operation in Python?
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line (20, 30, 40, 50, 60). Do not add data to file2.txt. Write a program. Create a new .py file that reads in the data from file1 and adds all together. Then output the sum to file2.txt. Add your name to the first line in file2.txt (see sample output) Sample Output Your Name 200 use a main function.
Python 3: I have a file with 53,000 entries or so of movies, the year they...
Python 3: I have a file with 53,000 entries or so of movies, the year they were made, the rating, length, and what genre they are. I need to get user input for the year, or the title, or the rating/genre/length. The genre is a 6 digit collection of 0's and 1's. The placement of 1's determines what genre they are (or a combination of genres). The genre,rating, and length are all one test and return entries that match all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT