0% found this document useful (0 votes)
20 views3 pages

First Chapter Project

The document describes a Python Time class that represents a time with hours and minutes. The class contains two methods: DisplayTime() displays the time in 12-hour format with AM/PM, handling invalid inputs. DisplayRatio() calculates the ratio of minutes to hours, handling division by zero. Both methods use try-except blocks to gracefully handle potential errors.

Uploaded by

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

First Chapter Project

The document describes a Python Time class that represents a time with hours and minutes. The class contains two methods: DisplayTime() displays the time in 12-hour format with AM/PM, handling invalid inputs. DisplayRatio() calculates the ratio of minutes to hours, handling division by zero. Both methods use try-except blocks to gracefully handle potential errors.

Uploaded by

Vignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What’s the time?

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.

Handle the error


This code attempts to perform a division operation on two numbers entered by the user.
It includes a try block that contains the code without any explicit exception handling. If
an error occurs during the execution of this code, it will be caught by the appropriate
except block.

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.

In summary, this code provides a basic example of exception handling in Python,


addressing potential errors that might occur during user input and arithmetic
operations.

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.

This code provides a straightforward way to calculate and display average


temperatures for two weeks.

Sum of first n Natural Numbers


● The user is prompted to enter a number (n).
● The input is converted to an integer using int(input(...)).
● A variable total is initialized to 0. This variable will store the sum of the
natural numbers.

● A for loop iterates through the range from 0 to n (inclusive).


● Inside the loop, each value of i is added to the total.

● The total sum of the natural numbers up to n is calculated.

● 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.

This code demonstrates a basic implementation of a bank account with simple


functionalities.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy