- This homework tests your knowledge of File Handling and use of Lists data type.
- Adhere to the naming conventions discussed in class for variable names, program name and function names
- Use meaningful names for variables, functions etc
- If there are two words in the variable use first word lowercase and first letter of second word upper case i.e., firstName or underscore between 2 words i.e first_name
- Include appropriate comments in the code
- Output should be displayed similar to as mentioned against each problem
- Indent the lines of code appropriately
1. Write and read file
a ) write_num.py (30 points)
Create a num_list file to write a range of numbers from 1 through 101.
Hints:
Define main ():
Open a file named as num_list in write mode.
Use for loop to write a range of numbers from 1 through 101 into the file.
Close file
b)read_num.py (40 points)
Use num_list file which got created in number 1.a. Write a program that will prompt the user to enter the name of the file (i.e. num_list.text), and display the first 10 lines of the files content. Use try/except construct to capture any file opening related errors such as invalid file name etc.
Hints:
Define main ():
Declare variables: line and counter, and initialize them
Prompt for file name: use input function
Open the file in read mode.
Read the lines in the file with readline function: inline.readline() and initialize counter to 1
Use a while loop to read and display the first 10 lines
Use rstrip method to remove n characters at the end of the line
Print the line
Update the counter when the line is read
Close file
Expected output:
Enter the name of the file: filename.txt
#
#
#
#
#
#
#
#
2. numanalysis.py (30 points)
Write a program that asks user to enter a series of 10 numbers. The program should store the numbers in a list then display the following data.
1. The list contents
2. The lowest number in the list
3. The highest number in the list
4. The total of the number in the list
5. The average of the number in the list
Hints:
Define main ():
Declare variables and initialize them
Create an empty list
Use the for loop with the range function to loop through 10 numbers.
Prompt the User to enter the numbers from 1- 10.
Ex: Enter number 1 of 10:
Use append function to populate the list
Use min, max, sum, functions to find the low , high and total number of the list.
Ex: low = min(number_list)
Print the list contents
Print Low, high , total and average numbers of the list.
Expected output :
List: [#, #, #, #, #, #, #, #, #, #]
Low: #.##
High: #.##
Total: #.##
Average: #.##