0% found this document useful (0 votes)
4 views2 pages

Python Practice Problems for Week 10

Uploaded by

wonjunha12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python Practice Problems for Week 10

Uploaded by

wonjunha12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[2024-2]

<Week 10> Practice problems

Create a Python script file (.py) for each problem, and compress them into a single zip file for
submission. Name the files as prob1-A, prob1-B, ..., prob6. Submit the compressed file to AJOU
Black board.
(각 문제마다 python script file(.py)을 만들고 하나로 압축하여 아주Bb에 제출하세요. 파일 이름은
prob1-A, prob1-B, ..., prob6로 만드세요.)

1. Write the following programs for the given [Link] file:


1-A. Write a program to read the entire content of the [Link] file as a single string.
1-B. Write a program to count the number of lines in the file.

2. Write a program to create 26 text files ([Link], [Link], … [Link]) at the current working directory.

3. Write a program to create ‘[Link]’ file at the current working directory and write 10 random
integers. The write function must convert the numbers to strings before writing them.

13
2
99

4. When reading data from a file, if the file does not exist, an IOError will occur. Let's handle this
using try and except. If the file does not exist, print 'File not found. Please try again.' on the
screen, and continue execution.

Enter a file name: [Link]


File not found. Please try again.
Enter a file name: [Link]
File successfully found.
5. The students' grades are stored as float numbers in the file [Link]. Read these grades from
the file and append the average value to the end of the file.

99.1 99.1
88.2 88.2
67.7 67.7
96.9 96.9
Avg: 87.975

6. Let's write a program that counts how many times each character appears in a file. We will use
a dictionary to count the occurrences of the characters. If you run the program with a file named
[Link], the output is as follows.

{'A': 2, 'l': 8, ' ': 19, 't': 9, 'h': 9, 'e': 14, 's': 8, 'i': 4, 'n': 6, 'o': 6, 'f': 2, 'a': 4, 'u': 3, 'd': 1, 'p': 1, 'g': 4, '\n':
3, 'r': 4, 'w': 1, 'm': 1, 'k': 1, 'y': 1, 'W': 1, 'v': 2, 'b': 2, 'N': 1}

You might also like