Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python program that will read the names contained in “names.txt” and ins

ID: 3699062 • Letter: W

Question

Write a Python program that will read the names contained in “names.txt” and insert them into a dictionary. They key values will be integers such that the first name will have key value 1, the second will have key value 2, the third will have key value 3 etc. Once the dictionary has been constructed, go through it and write the names that have prime key values into a new file called “namesprime.txt”. That is, only the names that have a key which is a prime number are written to “namesprime.txt”. You will have to create a function to determine whether an integer is prime or not. One way is to create an array of primes between 2 and 57 and check the value against those values. Another way is to go through all integers less than the key value and see if they divide it (really you only need to check integers from 2 to sqrt(number) to determine if it’s prime).

Explanation / Answer

First WE have to create the libarary in this to read name.txt file .

he first thing you’ll need to do is use Python’s built-in open function to get a file object.

When you use the open function, it returns something called a file object. File objects contain methods and attributes that can be used to collect information about the file you opened. They can also be used to manipulate said file

Reading a Text File in Python

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote