

from car import Car

my_car = Car("Audi")
print(my_car.make)



from car import Car, ElectricCar

my_car = Car("Audi")
my_electric_car = ElectricCar("BYD", 10000)


import car

my_car = car.Car("Ford")



# electric_car.py
from car import Car

class ElectricCar(Car): #inheritance based on the imported Car class
    def __init__(self, make, battery):
        super().__init__(make)
        self.battery = battery


from car import ElectricCar as EC
leaf = EC("Nissan", 1000)


import car as c
car = c.ElectricCar("Tesla", 1000)


################## File reading

from pathlib import Path

path = Path('pi_digits.txt')
contents = path.read_text()
print(contents)

path = Path('pi_digits.txt')
contents = path.read_text()
contents = contents.rstrip()
contents = contents.splitlines()
print(contents)


from pathlib import Path

path = Path('output.txt')
path.write_text("Hello from Python!\n")


contents = "Line 1\n"
contents += "Line 2\n"

path.write_text(contents)


import json
from pathlib import Path

settings = {
"language": "en",
"theme": "dark"
}

path = Path("settings.json")
json_text = json.dumps(settings)
path.write_text(json_text)


import json
from pathlib import Path


path = Path("settings.json")
json_text = path.read_text()
settings = json.loads(json_text)

print(settings["language"])
print(settings["theme"])



print(5 / 0)


try:
    print(5 / 0)
except ZeroDivisionError:
    print("You can't divide by zero!")



try:
    result = 10 / 2
except ZeroDivisionError:
    print("Error!")
else:
    print(result)

try:
    n = int(input("Enter a number: "))
except ValueError:
    print("That's not a number.")


from pathlib import Path

path = Path("unknown.txt")

try:
    text = path.read_text()
except FileNotFoundError:
    print("File not found.")


try:
    text = path.read_text()
except FileNotFoundError:
    print("File missing.")
else:
    print(text)



try:
    number = int(input("Enter number: "))
    result = 10 / number
except ValueError:
    print("Please enter a valid integer.")
except ZeroDivisionError:
    print("Number cannot be zero.")



"""
Exercises
"""

# Task 1 — Create the Project File
"""
Create a file called notes_app.py.
Add a simple print statement:
print("Notes App starting…")
"""

# Task 2 — Create the Notes List
"""
Add a variable:
notes = []
This list will store the notes.
"""

# Task 3 — Add User Input for One Note
"""
Ask the user for a note:
note = input("Enter a note: ")
Append it to the list:
notes.append(note)
"""

# Task 4 — Save Notes to a Text File
"""
Write all notes to notes.txt, one per line.
Use Path.write_text().
Example:
from pathlib import Path
path = Path("notes.txt")
contents = "\n".join(notes)
path.write_text(contents)
"""

# Task 5 — Load Notes from Text File
"""
At startup, try reading notes.txt.
If the file exists:
    contents = path.read_text()
    notes = contents.splitlines()
If not:
    notes = []
"""

# Task 6 — Add JSON Storage
"""
Convert list to dict:
data = {"notes": notes}
Use json.dumps() and write_text():
json_text = json.dumps(data)
Path("notes.json").write_text(json_text)
"""

# Task 7 — Load From JSON Safely
"""
Try loading notes.json:
json_text = Path("notes.json").read_text()
data = json.loads(json_text)
notes = data["notes"]
If missing, fall back to Task X-5 logic.
"""

# Task 8 — Menu System
"""
Create a loop, then ask for the user input in form of numbers.:
print("1 = View notes")
print("2 = Add note")
print("3 = Save notes")
print("4 = Exit")
Use if/elif to handle each action.
"""

# Task 9 — Handle Bad Input
"""
Protect integer conversion:
try:
    choice = int(input("Choose: "))
except ValueError:
    print("Please enter a number.")
"""

# Task 10 — Append vs Overwrite Option
"""
Add two save modes:
Option 3: overwrite notes.json entirely.
Option 4: append new notes:
    - load json
    - extend list
    - save again
"""

# Task 11 — FileNotFound Safety
"""
Use FileNotFoundError:
try:
    json_text = Path("notes.json").read_text()
except FileNotFoundError:
    notes = []
"""

# Task 12 — Final Polishing
"""
Add:
- Goodbye message
- Save notes automatically before exiting
- Print number of loaded notes at startup
Example:
print(f"Loaded {len(notes)} notes.")
"""
