With this code, you could ask the AI any basic math problem like 998+18 or 38X12 and it gives you the correct answer!

In order to run the code, copy the code below and paste it into your python file, then run the code and enjoy!

Copy the code for Free!:

import pygame
pygame.init()

# Set up the display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(“Math Chat”)

# Define colors
white = (255, 255, 255)
black = (0, 0, 0)

# Define fonts
font = pygame.font.Font(None, 24)

# Chat box
chat_box = pygame.Rect(50, 50, screen_width – 100, screen_height – 200)
input_box = pygame.Rect(50, screen_height – 100, screen_width – 100, 32)
color_inactive = pygame.Color(‘lightskyblue3’)
color_active = pygame.Color(‘dodgerblue2’)
color = color_inactive
active = False
text = ”
conversation = [] # To store conversation history
scroll_offset = 0 # To manage scrolling
text_surface = font.render(text, True, color)
width = max(200, text_surface.get_width()+10)
input_box.w = width

# AI response function
def generate_ai_response(user_input):
# Replace “X” with “*” for multiplication
user_input = user_input.replace(“X”, “*”)
try:
result = eval(user_input)
return “AI: ” + str(result)
except:
return “AI: Invalid input”

# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
# Process user input and generate AI response
user_msg = “You: ” + text
ai_msg = generate_ai_response(text)
conversation.append((user_msg, white))
conversation.append((ai_msg, black))
text = ”
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
text_surface = font.render(text, True, color)
width = max(200, text_surface.get_width()+10)
input_box.w = width

# Clear the screen
screen.fill(white)

# Draw chat box and input
pygame.draw.rect(screen, black, chat_box, 2)
pygame.draw.rect(screen, black, input_box, 2)
screen.blit(text_surface, (input_box.x+5, input_box.y+5))

# Display conversation with scrolling
y_offset = 10 – scroll_offset
for msg, msg_color in reversed(conversation):
msg_surface = font.render(msg, True, msg_color)
screen.blit(msg_surface, (chat_box.x+5, chat_box.y+y_offset))
y_offset += msg_surface.get_height() + 5
if y_offset >= chat_box.height – 10:
break

# Scroll up
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
scroll_offset = min(scroll_offset + 10, y_offset – chat_box.height + 20)

pygame.display.flip()

pygame.quit()