Problem Statement:
You are tasked with writing a Python script to read the contents of a text file named sample.txt
and display them on the screen. The file contains a list of names, each on a new line. Your program should also count the number of names in the file and print this count at the end.
Example Input (sample.txt):
Alice
Bob
Charlie
Diana
Example Output:
Alice
Bob
Charlie
Diana
Total names in the file: 4
Problem Statement:
Create a Python program that takes a list of student scores, writes them to a file named scores.txt
, with each score on a new line. Then, calculate the average score and append this information at the end of the file.
Example Output (scores.txt):
95
78
88
92
67
Average Score: 84.0
Problem Statement:
Write a Python script to reverse the contents of a text file named story.txt
and save the reversed content into a new file named reversed_story.txt
. The script should reverse the order of the lines, not the text within each line.
Example Input (story.txt):
Once upon a time,
there was a brave knight,
who fought dragons.
Example Output (reversed_story.txt):
who fought dragons.
there was a brave knight,
Once upon a time,
Problem Statement:
Develop a Python program that reads a text file named input.txt
and creates a new file named numbered_input.txt
where each line from the original file is prefixed with its line number followed by a colon.
Example Input (input.txt):
Hello, World!
This is a test file.
Python programming is fun.
Example Output (numbered_input.txt):
1: Hello, World!
2: This is a test file.
3: Python programming is fun.
Problem Statement:
Create a Python script that reads a text file named essay.txt
, counts the unique words in the file, and writes the count to a new file named unique_words_count.txt
. Words are considered the same if they are case-insensitive matches.
Example Input (essay.txt):
Python is great. Python is easy. Python is powerful.
Example Output (unique_words_count.txt):
Unique words count: 5
Question: Write a Python program that asks the user to input two numbers and then divides the first number by the second number. Your program should handle cases where the user might input a value that causes a division by zero error (ZeroDivisionError
).
Example:
If the user inputs 8
and 0
, the program should print "Cannot divide by zero!" instead of crashing.
Expected output for inputs 8
and 2
should be 4.0
.
Question: Create a Python script that prompts the user to enter a number and then prints the square of that number. Use exception handling to catch cases where the input cannot be converted to a number (ValueError
).
Example:
If the user inputs "a"
, the program should print "Please enter a valid number." instead of crashing.
Expected output for input 4
should be 16
.
Question: Suppose you have a list of numbers, for example, numbers = [1, 2, 3, 4, 5]
. Write a program that asks the user for an index and prints the element at that index in the list. Your program should handle cases where the user requests an index out of the list's bounds (IndexError
).
Example:
If the user inputs 10
, the program should print "Index is out of bounds!" since there is no element at index 10.
Expected output for input 2
should be 3
.
Question: Write a Python program to open a file named "data.txt" and print its contents to the console. Use exception handling to catch and handle the case where the file does not exist (FileNotFoundError
).
Example:
If "data.txt" does not exist in the program's directory, the program should print "File not found!" instead of crashing.
If "data.txt" exists and contains "Hello, world!", the program should print "Hello, world!".
Question: Create a Python program that converts a list of strings into integers. The list is ["10", "20", "hello", "40"]
. Use exception handling to skip over any values that cannot be converted to an integer and print the rest as integers.
Example:
Given the list ["10", "20", "hello", "40"]
, the program should print:
10
20
Cannot convert 'hello' to an integer.
40
Question: Modify the division program from Problem 6 to include handling for non-numeric inputs. The program should ask the user for two numbers and divide the first by the second, handling both division by zero and non-numeric inputs.
Example:
If the user inputs 9
and three
, the program should print "Please enter only numbers." instead of crashing.
Expected output for inputs 9
and 3
should be 3.0
.
Question: Write a Python script that accesses a value in a dictionary using a key provided by the user. Use a dictionary like data = {"name": "John", "age": 30}
. Your program should handle the case where the key does not exist in the dictionary (KeyError
).
Example:
If the user inputs occupation
, the program should print "Key not found!" since there is no occupation
key in the dictionary.
Expected output for input name
should be John
.