Artificial Intelligence (AI)
Assignment – 2 (Turtle Programming)
Supervisor: Muhammad Bilal Ashiq
Email: bilal@entracloud.net
Total Marks: 100
Turtle in Python
“Turtle” is a Python feature like a drawing board, which
lets us command a turtle to draw all over it! We can use
functions like turtle.forward(…) and turtle.right(…)
which can move the turtle around.
Q.No.01
Run this code. Python file
import turtle #Inside_Out
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Did you enjoy?
Paste Output here
Q.No.02
Run this Code, Python file
# Python program to draw
# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
t.pencolor(colors[x%6])
t.width(x//100 + 1)
t.forward(x)
t.left(59)
Did you enjoy?
Paste Output here
Q.No.03
Now follow this video and make a green color
heart. 💚
https://www.youtube.com/watch?
v=YmlJ789CZ58&list=PLi0EGg6K7IY0RTa
IJkdAll-ZFYnDrRD0-
Code:
from turtle import *
color('green' )
begin_fill()
left(50)
forward(100)
circle(40,180)
left(260)
circle(40,180)
forward(100)
end_fill()
done()
Q.No.04
Make a Square now.
Code 01
import turtle
# move turtle forward with
# distance = 100
turtle.forward(100)
Code 2
import turtle
turtle.forward(50) # move the turtle forward by 50
turtle.right(90) # change the direction
turtle.forward(50) # move the turtle forward by 50 again
Code:
from turtle import *
color('green')
begin_fill()
for _ in range(4):
forward(100)
right(90)
end_fill()
done()
Q.No.05
Make me a simple menu driven calculator on
Python. I know its code is available
everywhere, but try to solve it yourself
otherwise 0 marks.
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y != 0:
return x / y
else:
return "Error! Division by zero is not possible"
def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
while True:
choice = input("Enter choice (1/2/3/4/5): ")
if choice == '5':
print("Exiting the calculator. Goodbye!")
break
if choice in ['1', '2', '3', '4']:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter numeric values.")
continue
if choice == '1':
print(f"The result is: {add(num1, num2)}")
elif choice == '2':
print(f"The result is: {subtract(num1, num2)}")
elif choice == '3':
print(f"The result is: {multiply(num1, num2)}")
elif choice == '4':
print(f"The result is: {divide(num1, num2)}")
else:
print("Invalid input. Please enter a number between 1 and
5.")
if __name__ == "__main__":
calculator()
output: