1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
DedPeter [7]
3 years ago
11

Write a program that reads in your question #2 Python source code file and counts the occurrence of each keyword in the file. Yo

ur program should prompt the user to enter the Python source code filename

Computers and Technology
1 answer:
scoray [572]3 years ago
7 0

Answer:

Here is the Python program:

import keyword  #module that contains list of keywords of python

filename = input("Enter Python source code filename: ") # prompts user to enter the filename of a source code

code = open(filename, "r") # opens the file in read mode

keywords = keyword.kwlist #extract list of all keywords of Python and stored it into keywords

dictionary = dict() #creates a dictionary to store each keyword and its number of occurrence in source code

for statement in code: # iterates through each line of the source code in the file

   statement = statement.strip() # removes the spaces in the statement of source code  

   words = statement.split(" ") #break each statement of the source code into a list of words by empty space separator

   for word in words:# iterates through each word/item of the words list  

       if word in keywords:#checks if word in the code is present in the keywords list of Python  

           if word in dictionary: #checks if word is already present in the dictionary

               dictionary[word] = dictionary[word] + 1 #if word is present in dictionary add one to the count of the existing word

           else: #if word is not already present in the dictionary  

               dictionary[word] = 1 #add the word to the dictionary and set the count of word to 1

for key in list(dictionary.keys()): #iterates through each word in the list of all keys in dictionary  

   print(key, ":", dictionary[key])# prints keyword: occurrences in key:value format of dict

Explanation:

The program is well explained in the comments attached with each line of the program.  

The program prompts the user to enter the name of the file that contains the Python source code. Then the file is opened in read mode using open() method.

Then the keyword.kwlist statement contains the list of all keywords of Python. These are stored in keywords.

Then a dictionary is created which is used to store the words from the source code that are the keywords along with their number of occurrences in the file.

Then source code is split into the lines (statements) and the first for loop iterates through each line and removes the spaces in the statement of source code .

Then the lines are split into a list of words using split() method. The second for loop is used to iterate through each word in the list of words of the source code. Now each word is matched with the list of keywords of Python that is stored in keywords. If a word in the source code of the file is present in the keywords then that word is added to the dictionary and the count of that word is set to 1. If the word is already present in the dictionary. For example if there are 3 "import" keywords in the source code and if 1 of the import keywords is already in the dictionary. So when the second import keyword is found, then the count of that keyword is increased by 1 so that becomes 2.

Then the last loop is used to print each word of the Python that is a keyword along with its number of occurrences in the file.

The program and its output is attached in a screenshot. I have used this program as source code file.

You might be interested in
What are some other projects or things you do in your life (other than writing an essay) where you use an iterative process?
Sophie [7]

Answer:The iterative design process occurs in a continuous cycle involving three unique stages: formulate, test, evaluate. These core elements make up the basic progression in which the development of a game will follow. The rest is simply rinse and repeat

Explanation:

7 0
3 years ago
Read 2 more answers
Alex works as a customer-service representative at an insurance company. Before starting his shift, Alex reviews issues from his
never [62]

Answer:

problem-solving

Explanation:

he is making sure all his problems have been resolved before starting a new shift.

4 0
3 years ago
Read 2 more answers
What is the output of this program? Assume the user enters 3, 6, and 11.
erica [24]

Answer:

14.0

Explanation:

3 0
1 year ago
Difference between volatile and non volatile memory
slega [8]
<span>Volatile memory requires electricity or some kind of current to store information, and nonvolatile memory does not.</span>
6 0
3 years ago
Read 2 more answers
Advertising andpublicity are used to:
WINSTONCH [101]

Answer:

d. All of the given options

Explanation:

Great question, it is always good to ask away and get rid of any doubts that you may be having.

<u>Advertising and Publicity</u> is used for many reasons.

Retail Companies use advertising and publicity to promote their products and in term increase sales of that product when it hits the shelves.

Meanwhile, other companies such as Law Firms use advertising and publicity create awareness for their law firm and communicate with customers over high profile case etc.

Therefore, the answer is all of the above.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

5 0
3 years ago
Other questions:
  • Can someone please help me withe these questions its really urgent
    5·1 answer
  • If the pc-doctor software is installed on a computer's hard drive, what two different ways can the program be started?
    9·1 answer
  • what program searches the Internet for specified keywords and returns a list of the pages where the keywords were found
    8·1 answer
  • Which of the following is true about images that are arranged in a collumn
    9·1 answer
  • What is the main advantage of using DHCP? A. Allows you to manually set IP addresses B. Allows usage of static IP addresses C. L
    8·1 answer
  • How can your microsoft word processing skills affect overall writing skills on the job?<br><br>​
    7·1 answer
  • I am needing help with this homework of mine.
    7·1 answer
  • Windows 8 has a built-in antivirus program called
    10·1 answer
  • 25 points select 3 options!!!!!!!!!!!!!!!!!!!!!!!!!
    8·1 answer
  • 4.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!