First Chapter Project
First Chapter Project
I've created a Python class called `Time` that represents a time with hours and minutes. The
class has two methods: `DisplayTime` and `DisplayRatio`.
In the `__init__` method, I initialize the object with provided values for hours and minutes. The
`DisplayTime` method is designed to display the time in a 12-hour format with AM or PM. It
includes error handling to check if the input values for hours and minutes are within the valid
range (0-23 for hours and 0-59 for minutes). If the input is invalid, it raises a `ValueError` with an
appropriate error message.
The `DisplayTime` method also calculates whether the time is in the AM or PM period and
adjusts the displayed hours accordingly. The result is printed in a formatted string.
The `DisplayRatio` method calculates and displays the ratio of minutes to hours. It checks if the
hours are zero and raises a `ZeroDivisionError` in such a case to prevent division by zero.
Otherwise, it calculates the ratio and prints it with two decimal places.
Both methods utilize a try-except block to catch potential errors and display corresponding error
messages. This helps ensure that the program handles unexpected inputs or situations
gracefully. Overall, this class is a simple representation of time with methods to display it in a
human-readable format and calculate a ratio.
User Input:
● The user is prompted to enter two numbers.
Division Operation:
● The code attempts to perform the division operation num1 / num2.
Exception Handling:
● If a ValueError occurs (e.g., if the user enters a non-integer), the first
except block is executed. It prints an error message indicating that invalid
input was provided.
● If a ZeroDivisionError occurs (e.g., if the user enters 0 as the second
number), the second except block is executed. It prints an error message
indicating that division by zero is not allowed.
● If any other exception occurs (caught by the generic Exception), the third
except block is executed. It prints a generic error message indicating that
an unexpected error occurred.
Average temperature
Two lists (temperatures_w1 and temperatures_w2) represent the temperatures for
week 1 and week 2, respectively.
● A for loop is used to iterate through each temperature in temperatures_w1.
● The sum of temperatures for week 1 (sum_w1) is calculated.
● The average temperature for week 1 (average_w1) is calculated by dividing the
sum by the number of temperatures.
The average temperature for week 1 is printed to the console.
● imilar to week 1, a for loop is used to iterate through each temperature in
temperatures_w2.
● The sum of temperatures for week 2 (sum_w2) is calculated.
● The average temperature for week 2 (average_w2) is calculated by dividing the
sum by the number of temperatures.
● The average temperature for week 2 is printed to the console.
● The result is printed to the console, indicating the sum of the first n
natural numbers.
Bank Account Management System
Class Definition:
● The BankAccount class is defined with an __init__ method for
initializing account details and a few other methods (deposit, withdraw,
and account_details) for managing the account.
Initialization:
● When creating a BankAccount instance (account1), the initial values for
name, account_number, and balance are set.
Deposit Method:
● The deposit method allows for depositing funds into the account. It
updates the account balance and prints a message with the deposited
amount and the new balance.
Withdraw Method:
● The withdraw method allows for withdrawing funds from the account,
provided there are sufficient funds. It updates the account balance and
prints a message with the withdrawn amount and the new balance. If
there are insufficient funds, an appropriate message is printed.
Account Details Method:
● The account_details method prints the account holder's name, account
number, and current balance.
Test Case:
● An instance of the BankAccount class is created (account1) with initial
values.
● Funds are deposited and withdrawn using the deposit and withdraw
methods.
● Finally, the account details are displayed using the account_details
method.