Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON 1. # [ ] Write an expression to raise a `SyntaxError` exception 2. # [ ] Write an expression to raise a `TypeError` exception

PYTHON

1. # [ ] Write an expression to raise a `SyntaxError` exception

2. # [ ] Write an expression to raise a `TypeError` exception

3. ## [ ] The following program divides the elements of `lst` by a float number `x` specified by the user

# Use exception handling to correct for the ALL generated exceptions

# When you handle all exceptions correctly, you should see the word "Done!" in the program output

# HINT: You might need to use 2 `try..except` statements

# Test cases:

# x = 5

# x = 6.3

# x = "number"

# x = 0

lst = [8, 85.4, [55, 4], 'word', (59,), {2:43.5}]

x = input("Enter a number: ")

x = float(x)

for i in range(7):

print("{} / {:.2f} = {:.2f}".format(lst[i], x, lst[i] / x))

print("Done!")

4. # [ ] The following program asks the user for a file name.

# The file is then opened, the first line printed out, then closed.

# Use exception handling to show the user a message confirming the file process was completed successfully

# your exception handling should also deal with the cases when the file does not exist

# test cases:

# fname = text_file.txt (should display: File process was completed successfully)

# fname = nofilehere.txt (should display: nofilehere.txt was not found)

import os

# ask user for a file name

fname = input("Enter file name: ")

# the file should be located in `parent_dir`

fname = os.path.join("parent_dir", fname)

# opening text_file.txt for reading

f = open(fname, 'r')

print(f.readline())

f.close()

5. # [ ] The following program tries to read from a file opened for writing.

# The program will terminate before closing the file, and the file resources will not be released.

# Use exception handling with a `finally` to make sure the file is closed

# Open a text file for writing

f = open("parent_dir/text_file_2.txt", 'w')

# trying to read from a file open for writing (will raise an exception)

print(f.readline())

# closing the file (will not be reached if an exception was raised)

f.close()

print("File closed")

6. # [ ] Write a program to keep prompting the user for a number from a predefined numerical tuple `valid_nums`

# Your program should raise an exception with an appropriate message if the input is not valid

valid_nums = (1, 2, 8, 16, 32, 64)

7.

# ************ DO NOT MODIFY OR ADD ANYTHING TO THIS CODE CELL ***********************

# ++++++++++++ This code segment must be run before attempting any of the tasks in this lesson.

# It prepares the directories and files necessary to complete the tasks.

import os, random, shutil

# Navigate to `parent_dir` directory (if not already in it)

current_path = os.getcwd()

if ("parent_dir" in current_path):

nb_path = current_path.split("parent_dir")[0]

else:

nb_path = current_path

print("Changing working dir to parent_dir")

os.chdir(os.path.join(nb_path,'parent_dir'))

print("Current working directory:", os.getcwd())

# Remove the `files_exercises` directory (if it exists)

if('files_exercises' in os.listdir()):

print('Removing "files_exercises"')

shutil.rmtree('files_exercises')

# Create a new directory called `files_exercises`

print('Making "files_exercises"')

os.mkdir('files_exercises')

# Change the working directory to `files_exercises`

print('Changing working directory to "files_exercises"')

os.chdir('files_exercises')

# Display the current working directory to verify you are in the correct location

print("Current working directory:", os.getcwd())

# Create 100 text files, the first line of each file is a random number in the range [1000, 9999]

print("Creating 100 text files")

random.seed(25000) # to get the same random numbers every time the setup runs

for i in range(100):

file_name = str(i) + ".txt"

f = open(file_name, 'w')

f.write(str(random.randint(1000, 9999)))

f.close()

# Create 5 directories

print("Creating 5 directories")

for i in range(1, 6):

os.mkdir("dir_"+str(i))

print("Environment setup completed!")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

What sector and functional experience are we looking for?

Answered: 1 week ago