Chapter 2

292 Words
Chapter 2: Creating the J.A.R.V.I.S. Core – The Heart of Your Assistant Now that your tools are ready and you've said your first “Hello, world,” it’s time to build the brain of your assistant—the core loop that listens to your commands and acts accordingly. --- 2.1 What Is a Command Loop? Think of it like a conversation. You say something → J.A.R.V.I.S. listens → Processes it → Responds or executes a task. In Python, this is done with a while loop that keeps running unless you tell it to stop. --- 2.2 Let's Code It Open your terminal and edit your script: nano jarvis.py Clear everything and enter this: import time print("Initializing J.A.R.V.I.S....") time.sleep(1) print("Hello, I am J.A.R.V.I.S. Online and ready.") # Start command loop while True: command = input("You: ").lower() if "time" in command: current_time = time.strftime("%I:%M %p") print("J.A.R.V.I.S.: The time is", current_time) elif "exit" in command or "bye" in command: print("J.A.R.V.I.S.: Going offline. Goodbye, sir.") break else: print("J.A.R.V.I.S.: Sorry, I didn't understand that.") --- 2.3 What This Does Greets the user Waits for input with input("You: ") Responds to keywords like "time" or "bye" Keeps running unless “exit” or “bye” is entered --- 2.4 Try It Out Run your script: python jarvis.py Try saying: What is the time? Exit Hello --- 2.5 Your First Custom Commands Let’s add a few more responses: elif "hello" in command: print("J.A.R.V.I.S.: Hello sir, how can I assist you today?") elif "who are you" in command: print("J.A.R.V.I.S.: I am your virtual assistant. Just like the one Tony Stark built.") Now it feels more personal—just like talking to your own digital companion. --- What’s Next? In Chapter 3, you’ll learn to make J.A.R.V.I.S. do real tasks:
Free reading for new users
Scan code to download app
Facebookexpand_more
  • author-avatar
    Writer
  • chap_listContents
  • likeADD