Simple Python Code
print("=== Smart Home AI (Decision-Making Rules) ===\n")
while True:
# Ask input from user about home conditions
motion = input("Is there motion? (yes/no): ").lower()
lights = input("Are lights on? (yes/no): ").lower()
door = input("Is door open? (yes/no): ").lower()
temp = input("Is temperature high? (yes/no): ").lower()
# AI decides what to do
print("\nSmart Home AI Actions:")
action_taken = False
if motion == "yes" and lights == "no":
print("- Turn lights ON")
action_taken = True
if motion == "no" and lights == "yes":
print("- Turn lights OFF")
action_taken = True
if door == "yes" and motion == "no":
print("- Lock the door")
action_taken = True
if temp == "yes":
print("- Cool the room")
action_taken = True
if not action_taken:
print("- System on standby")
print()
# Ask user to continue
cont = input("Continue? (yes/no): ").lower()
if cont != "yes":
print("Smart Home shutting down.")
break
Advanced Python Code
class SmartHomeAI:
def __init__(self):
self.state = "idle"
self.power_mode = "normal"
def perceive(self):
print("\nUpdate Home Status:")
motion = input("Motion detected? (yes/no): ").lower() == "yes"
temp_high = input("Is the temperature too high? (yes/no): ").lower() == "yes"
door_open = input("Is the main door open? (yes/no): ").lower() == "yes"
lights_on = input("Are lights currently ON? (yes/no): ").lower() == "yes"
return {
"motion": motion,
"temp_high": temp_high,
"door_open": door_open,
"lights_on": lights_on
}
def decide(self, env):
actions = []
if env["door_open"] and not env["motion"]:
actions.append("lock_door")
if env["motion"] and not env["lights_on"]:
actions.append("turn_on_lights")
if not env["motion"] and env["lights_on"]:
actions.append("turn_off_lights")
if env["temp_high"]:
actions.append("cool_room")
if not actions:
actions.append("standby")
return actions
def act(self, actions):
actions_map = {
"lock_door": ("Door locked.", "securing home"),
"turn_on_lights": ("Lights turned ON.", "lighting home"),
"turn_off_lights": ("Lights turned OFF.", "saving energy"),
"cool_room": ("Cooling system activated.", "cooling home"),
"standby": ("No action needed.", "idle")
}
messages = []
states = []
for action in actions:
message, new_state = actions_map[action]
messages.append(f"Action: {action} | {message}")
states.append(new_state)
self.state = ", ".join(states)
return " | ".join(messages) + f" | State: {self.state}"
print("=== Smart Home AI Simulation ===")
ai = SmartHomeAI()
while True:
env = ai.perceive()
actions = ai.decide(env)
print("\nSmart Home:", ai.act(actions))
cont = input("\nContinue simulation? (yes/no): ").lower()
if cont != "yes":
print("\nSmart Home AI shutting down.")
break