9) b. Main Function Examples




A file is given to Gnome but he is unaware as to what to do to make the code begin from ‘start’ method. Write a program for him to make that happen. Please note that Gnome would be running main.py directly and would not want this to run if this module is imported in some other file.

1
2
3
4
Output:
starting the program
in midst of the program
end of the program




Ultra Cat claims to have a magical ability to quickly add 2 numbers. It astonishes everyone that meets her. Bella “Cutthroat” Evans is jealous of her and wants to find out how she is able to add the numbers quickly. He sneaks into Ultra Cat’s computer and comes across the following python module. Write the main method that can take two inputs and test the method.

1
2
3
4
Output:
enter two numbers separated by space
 23 53
the sum is 76




Jane has gotten access of Rory’s, her ex’s, computer and wants to know what he has on his system. She knows Rory keeps all the data in a specific directory. Write a program that lists all the files in a directory. Also write a test case to test the same..

1
2
3
4
5
6
7
8
9
10
Program:
#directory_list.py
import os
import pathlib
def list_directory(path):
    print('\n'.join(os.listdir(path)))

if __name__=='__main__':
    print('Testing the program')
    list_directory((pathlib.Path.cwd()))