Skip to content

Commit 0424bfe

Browse files
committed
update 15th april
1 parent 7825c28 commit 0424bfe

File tree

8 files changed

+231
-0
lines changed

8 files changed

+231
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class FormulaError(Exception):
2+
pass
3+
4+
def calculate():
5+
try:
6+
user_input = input("Enter a number, an operator (+ or -), and another number (separated by spaces): ")
7+
inputs = user_input.split()
8+
9+
if len(inputs) != 3:
10+
raise FormulaError("Invalid input. Input must consist of 3 elements.")
11+
12+
num1 = float(inputs[0])
13+
operator = inputs[1]
14+
num2 = float(inputs[2])
15+
16+
if operator not in ['+', '-']:
17+
raise FormulaError("Invalid operator. Operator must be '+' or '-'.")
18+
19+
if operator == '+':
20+
result = num1 + num2
21+
else:
22+
result = num1 - num2
23+
24+
print("Result:", result)
25+
26+
except ValueError:
27+
raise FormulaError("Invalid input. Numbers must be valid.")
28+
29+
except FormulaError as e:
30+
print("FormulaError:", e)
31+
32+
calculate()

FileNotFoundError.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def open_file(filename):
2+
try:
3+
file = open(filename, 'r')
4+
contents = file.read()
5+
print("File contents:")
6+
print(contents)
7+
file.close()
8+
except FileNotFoundError:
9+
print("Error: File not found.")
10+
11+
file_name = input("Input a file name: ")
12+
open_file(file_name)

IndexError.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def test_index(data, index):
2+
try:
3+
result = data[index]
4+
print("Result:", result)
5+
except IndexError:
6+
print("Error: Index out of range.")
7+
8+
nums = [1, 2, 3, 4, 5, 6, 7]
9+
index = int(input("Input the index: "))
10+
test_index(nums, index)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import math
2+
3+
try:
4+
number = float(input("Enter a number: "))
5+
if number < 0:
6+
raise ValueError("Negative numbers are not allowed.")
7+
result = math.sqrt(number)
8+
with open("sqrt_results.txt", "w") as file:
9+
file.write(f"The square root of {number} is {result}")
10+
print(f"The square root of {number} is {result}")
11+
except ValueError as e:
12+
print(f"Error: {e}")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Summary
2+
The code snippet is a Python program that simulates a simple bank account system. It defines a class called `BankAccount` which has methods for depositing, withdrawing, and checking the balance of an account. The program also includes a function to create a new account and a main function to handle user input and perform the desired operations on the accounts.
3+
4+
## Example Usage
5+
```python
6+
1. Create Account
7+
2. Deposit
8+
3. Withdraw
9+
4. Check Balance
10+
5. Exit
11+
Enter your choice: 1
12+
Enter account number: 123456
13+
Enter initial balance: 1000
14+
Enter account holder's name: John Doe
15+
Account created successfully.
16+
17+
1. Create Account
18+
2. Deposit
19+
3. Withdraw
20+
4. Check Balance
21+
5. Exit
22+
Enter your choice: 2
23+
Enter account number: 123456
24+
Enter deposit amount: 500
25+
Deposited 500 into account 123456.
26+
27+
1. Create Account
28+
2. Deposit
29+
3. Withdraw
30+
4. Check Balance
31+
5. Exit
32+
Enter your choice: 3
33+
Enter account number: 123456
34+
Enter withdrawal amount: 200
35+
Withdrew 200 from account 123456.
36+
37+
1. Create Account
38+
2. Deposit
39+
3. Withdraw
40+
4. Check Balance
41+
5. Exit
42+
Enter your choice: 4
43+
Enter account number: 123456
44+
Account 123456 balance: 1300
45+
46+
1. Create Account
47+
2. Deposit
48+
3. Withdraw
49+
4. Check Balance
50+
5. Exit
51+
Enter your choice: 5
52+
Exiting...
53+
```
54+
55+
## Code Analysis
56+
### Inputs
57+
- `account_number`: a string representing the account number
58+
- `initial_balance`: a float representing the initial balance of the account
59+
- `account_holder`: a string representing the name of the account holder
60+
- `amount`: a float representing the amount to deposit or withdraw
61+
- `choice`: a string representing the user's choice of operation
62+
___
63+
### Flow
64+
1. The program starts by defining a class called `BankAccount` with methods for depositing, withdrawing, and checking the balance of an account.
65+
2. The `create_account` function prompts the user to enter the account number, initial balance, and account holder's name, and returns a new `BankAccount` object with the provided information.
66+
3. The `main` function initializes an empty list called `accounts` and enters a loop to repeatedly prompt the user for their choice of operation.
67+
4. If the user chooses to create an account (choice 1), the `create_account` function is called and the resulting account object is appended to the `accounts` list.
68+
5. If the user chooses to deposit (choice 2), the program prompts for the account number and deposit amount, and then searches for the corresponding account in the `accounts` list. If found, the `deposit` method of the account object is called with the provided amount.
69+
6. If the user chooses to withdraw (choice 3), the program prompts for the account number and withdrawal amount, and then searches for the corresponding account in the `accounts` list. If found, the `withdraw` method of the account object is called with the provided amount.
70+
7. If the user chooses to check the balance (choice 4), the program prompts for the account number and searches for the corresponding account in the `accounts` list. If found, the `check_balance` method of the account object is called.
71+
8. If the user chooses to exit (choice 5), the program breaks out of the loop and terminates.
72+
9. If the user enters an invalid choice, an error message is displayed.
73+
___
74+
### Outputs
75+
- Messages indicating the success or failure of account creation, deposit, withdrawal, and balance checking operations.
76+
___
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class Account:
2+
def __init__(self, number, balance, holder):
3+
self.number = number
4+
self.balance = balance
5+
self.holder = holder
6+
7+
def deposit(self, amount):
8+
if amount > 0:
9+
self.balance += amount
10+
print(f"Deposited {amount} into account {self.number}.")
11+
else:
12+
print("Invalid deposit amount.")
13+
14+
def withdraw(self, amount):
15+
if amount > 0:
16+
if self.balance >= amount:
17+
self.balance -= amount
18+
print(f"Withdrew {amount} from account {self.number}.")
19+
else:
20+
print("Insufficient funds.")
21+
else:
22+
print("Invalid withdrawal amount.")
23+
24+
def check_balance(self):
25+
print(f"Account {self.number} balance: {self.balance}")
26+
27+
28+
def create_account():
29+
number = input("Enter account number: ")
30+
balance = float(input("Enter initial balance: "))
31+
holder = input("Enter account holder's name: ")
32+
return Account(number, balance, holder)
33+
34+
35+
def main():
36+
accounts = []
37+
while True:
38+
print("\n1. Create Account")
39+
print("2. Deposit")
40+
print("3. Withdraw")
41+
print("4. Check Balance")
42+
print("5. Exit")
43+
choice = input("Enter your choice: ")
44+
45+
if choice == "1":
46+
account = create_account()
47+
accounts.append(account)
48+
print("Account created successfully.")
49+
50+
elif choice == "2":
51+
number = input("Enter account number: ")
52+
amount = float(input("Enter deposit amount: "))
53+
for account in accounts:
54+
if account.number == number:
55+
account.deposit(amount)
56+
break
57+
else:
58+
print("Account not found.")
59+
60+
elif choice == "3":
61+
number = input("Enter account number: ")
62+
amount = float(input("Enter withdrawal amount: "))
63+
for account in accounts:
64+
if account.number == number:
65+
account.withdraw(amount)
66+
break
67+
else:
68+
print("Account not found.")
69+
70+
elif choice == "4":
71+
number = input("Enter account number: ")
72+
for account in accounts:
73+
if account.number == number:
74+
account.check_balance()
75+
break
76+
else:
77+
print("Account not found.")
78+
79+
elif choice == "5":
80+
print("Exiting...")
81+
break
82+
83+
else:
84+
print("Invalid choice.")
85+
86+
87+
if __name__ == "__main__":
88+
main()

sqrt_results.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The square root of 3.0 is 1.7320508075688772

0 commit comments

Comments
 (0)