Question: Write Python code: Write a program that reads a Python source file and prints out the number of function definitions found in the source file.

Write Python code:

Write a program that reads a Python source file and prints out the number of function definitions found in the source file. The task is accomplished by counting how many times you detect the reserved word def in a given file. Assume the input file is a valid Python program and does not include any comments. Use this starter Python code:

def count_def(source): print(source) ## test print;

return 10 ## MODIFY this

while True: filename = input("Python Source File: ")

if filename == "": break

file = open(filename, "r")

source = file.read()

def_cnt = count_def(source)

print(def_cnt)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!