Question: Programming Langue for all questions is Python Question 1 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Consider a Circle class used to represent circle objects. Instances of the Circle class will
Programming Langue for all questions is Python
Question 1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Consider a Circle class used to represent circle objects. Instances of the Circle class will have an attribute called radius which indicates the size of the circle. The constructor method (e.g. __init__) for this class will initialise this attribute as usual. Clearly, it doesn't make sense for a Circle object to have a size less than or equal to 0. If someone attempts to create a Circle object with a negative or zero radius, then you should raise an exception of type ValueError. The ValueError object should be created with the following string: Radius must not be less than or equal to 0
In addition, If someone attempts to create a Circle object with a non-integer valued radius, then you should raise an exception of type TypeError. The ValueError object should be created with the following string: Radius must be an integer value
Define a Circle class with a constructor method that prevents Circle object being created with an invalid radius. The __repr__ and __str__ functions of this class should return the following string:
Circle(x)
where x is the radius of the circle.
For example:
| Test | Result |
|---|---|
try: c = Circle(10) except ValueError as x: print("Error: " + str(x)) else: print(c) | Circle(10) |
try: c = Circle(-10) except ValueError as x: print("Error: " + str(x)) else: print(c) | Error: Radius must not be less than or equal to 0 |
try: c = Circle('25') except TypeError as x: print("Error: " + str(x)) else: print(c) | Error: Radius must be an integer value |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question 2
For this exercise, you will define a function that calculates the square root of x divided by y, as illustrated in the image below:
Your function should begin with the definition as follows:
def square_root_divide(x, y):
This would be a very straightforward exercise if you could always trust the user calling the "square_root_divide" function to pass in sensible input values for x and y. However, because you can't trust the user, there are a few things that could go wrong:
if you try to calculate the square root of a negative number, you will get a ValueError exception
if you try to divide one number by 0, you will get a ZeroDivisionError exception
if either x or y is not a number (e.g. maybe x is a string), you will get a TypeError
Complete the definition of the square_root_divide function, and make use of exception handling (using the try-except syntax) so that the code will not crash in any of the cases listed above. If the calculation is successful, you should return the correct calculated value. However, if an exception occurs, then you should return a string that indicates what went wrong. For the three exception types listed above, your function should return the following strings exactly as shown below if the corresponding exception occurs:
negative square root
division by zero
operands not numbers
Note: you can assume that "import math" has been called before your function is executed, so that the math.sqrt() function is available
For example:
| Test | Result |
|---|---|
x = square_root_divide(125, 5) print(x) | 5.0 |
x = square_root_divide(-125, 5) print(x) | negative square root |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question 3
The following short program will open a file (on disk) called "input.txt" and then display the contents of that file: input_file = open("input.txt", "r") data = input_file.read() print(data) input_file.close() An IOError exception occurs when a file cannot be opened (for example, this may be because the file name provided is not correct). Write a small program (not a function this time) which gets the user to type in the name of a file and which then displays the contents of that file. If the filename is not valid, then you should print an error message "File not found".
Your program should begin with the line (note there is a space after the ":"):
file_str = input("Enter a filename: ")
and you should then attempt to open the file referred to by the variable file_str, and print its contents.
IMPORTANT NOTE: Whether or not the program can successfully open the file, your program should print "The end" as the very last line of output. To do this, use a finally clause at the end of your try statement.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
