|
| 1 | +import json, os |
| 2 | + |
| 3 | +# Save the JSON file in the current working directory |
| 4 | +DATA_FILE = os.path.join(os.getcwd(), "tree_data.json") |
| 5 | + |
| 6 | +def load_data(): |
| 7 | + if os.path.exists(DATA_FILE): |
| 8 | + with open(DATA_FILE, "r") as f: |
| 9 | + return json.load(f) |
| 10 | + return {"total": 0, "goal": 50} |
| 11 | + |
| 12 | +def save_data(data): |
| 13 | + with open(DATA_FILE, "w") as f: |
| 14 | + json.dump(data, f) |
| 15 | + |
| 16 | +def show_progress(total, goal): |
| 17 | + percent = min(100, int((total / goal) * 100)) |
| 18 | + filled = percent // 5 |
| 19 | + bar = "█" * filled + "-" * (20 - filled) |
| 20 | + print(f"\nProgress: [{bar}] {percent}% ({total}/{goal} trees)") |
| 21 | + forest = "🌲" * min(total, 50) |
| 22 | + print(forest or "No trees yet! Start planting 🌱") |
| 23 | + |
| 24 | +def plant_trees(data): |
| 25 | + try: |
| 26 | + n = int(input("How many trees do you want to plant? 🌳 -> ")) |
| 27 | + if n <= 0: |
| 28 | + print("Please enter a positive number!") |
| 29 | + return |
| 30 | + except ValueError: |
| 31 | + print("Invalid input.") |
| 32 | + return |
| 33 | + data["total"] += n |
| 34 | + save_data(data) |
| 35 | + print(f"\n✅ You planted {n} tree(s)! Total: {data['total']}") |
| 36 | + if data["total"] >= data["goal"]: |
| 37 | + print("🎉 Congratulations! You've reached your goal!\n") |
| 38 | + show_progress(data["total"], data["goal"]) |
| 39 | + |
| 40 | +def set_goal(data): |
| 41 | + try: |
| 42 | + g = int(input("Enter your new tree goal 🌿 -> ")) |
| 43 | + if g <= 0: |
| 44 | + print("Goal must be positive!") |
| 45 | + return |
| 46 | + except ValueError: |
| 47 | + print("Invalid input.") |
| 48 | + return |
| 49 | + data["goal"] = g |
| 50 | + save_data(data) |
| 51 | + print(f"🎯 New goal set to {g} trees!\n") |
| 52 | + |
| 53 | +def reset_data(): |
| 54 | + confirm = input("Type 'RESET' to clear all data: ") |
| 55 | + if confirm.strip().upper() == "RESET": |
| 56 | + save_data({"total": 0, "goal": 50}) |
| 57 | + print("⚠️ Data reset successfully!\n") |
| 58 | + else: |
| 59 | + print("Reset cancelled.\n") |
| 60 | + |
| 61 | +def main(): |
| 62 | + print("\n🌲 Welcome to Virtual Tree Planter 🌲") |
| 63 | + data = load_data() |
| 64 | + while True: |
| 65 | + print("\nMenu:") |
| 66 | + print("1️⃣ Plant Trees") |
| 67 | + print("2️⃣ Show Progress") |
| 68 | + print("3️⃣ Set Goal") |
| 69 | + print("4️⃣ Reset Data") |
| 70 | + print("5️⃣ Exit") |
| 71 | + choice = input("Choose an option (1-5): ") |
| 72 | + |
| 73 | + if choice == "1": |
| 74 | + plant_trees(data) |
| 75 | + elif choice == "2": |
| 76 | + show_progress(data["total"], data["goal"]) |
| 77 | + elif choice == "3": |
| 78 | + set_goal(data) |
| 79 | + elif choice == "4": |
| 80 | + reset_data() |
| 81 | + data = load_data() |
| 82 | + elif choice == "5": |
| 83 | + print("\nGoodbye! Keep planting 🌳") |
| 84 | + break |
| 85 | + else: |
| 86 | + print("Invalid option. Try again!") |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments