-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment 1.py
More file actions
51 lines (43 loc) · 1.43 KB
/
assignment 1.py
File metadata and controls
51 lines (43 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 13 19:26:07 2025
@author: pkhan
"""
# simple_chatbot_basic.py
print("Hello! I'm a simple chatbot. Type 'goodbye' to exit.")
while True:
user_input = input("> ").lower()
# Exit condition
if "goodbye" in user_input or "bye" in user_input:
print("Goodbye!")
break
# Greeting
elif "hello" in user_input or "hi" in user_input:
print("Hello there!")
# Math: addition
elif "plus" in user_input:
parts = user_input.split()
if len(parts) >= 5 and parts[0] == "what" and parts[1] == "is":
try:
num1 = int(parts[2])
num2 = int(parts[4])
print(num1 + num2)
except ValueError:
print("I can only add numbers!")
else:
print("Try: What is 3 plus 4?")
# Math: multiplication
elif "times" in user_input:
parts = user_input.split()
if len(parts) >= 5 and parts[0] == "what" and parts[1] == "is":
try:
num1 = int(parts[2])
num2 = int(parts[4])
print(num1 * num2)
except ValueError:
print("I can only multiply numbers!")
else:
print("Try: What is 3 times 4?")
# Unknown input
else:
print("I don't understand. Try saying hello or ask a math question.")