0% found this document useful (0 votes)
8 views6 pages

Python Graphics and Validation Functions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Python Graphics and Validation Functions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week-6:

1. a. Write a function called draw_rectangle that takes a Canvas and a Rectangle as


arguments and draws a representation of the Rectangle on the Canvas.

Note: To install swampy in command prompt

#pip install swampy


from [Link] import *
class Canvas(object):
"""Represents a canvas.
attributes: width, height, background color"""
a_canvas = Canvas()
a_canvas.width = 500
a_canvas.height = 500
class Rectangle(object):
"""Represents a rectangle."""
box = Rectangle()
[Link] = 'orange'
[Link] = [[-100, -60],[100, 60]]
def draw_rectangle(canvas, rectangle):
drawn_canvas = [Link]([Link], [Link])
drawn_canvas.rectangle([Link], fill=[Link])

world = World()
draw_rectangle(a_canvas,box)
[Link]()
1. b. Add an attribute named color to your Rectangle objects and modify draw_rectangle
so that ituses the color attribute as the fill color.

from [Link] import *


class Canvas(object):
"""Represents a canvas.
attributes: width, height, background color"""
a_canvas = Canvas()
a_canvas.width = 500
a_canvas.height = 500
class Rectangle(object):
"""Represents a rectangle."""
box = Rectangle()
[Link] = 'orange'
[Link] = [[-100, -60],[100, 60]]
def draw_rectangle(canvas, rectangle):
drawn_canvas = [Link]([Link], [Link])
drawn_canvas.rectangle([Link], fill=[Link])

world = World()
draw_rectangle(a_canvas,box)
[Link]()
1. c. Write a function called draw_point that takes a Canvas and a Point as arguments and
draws arepresentation of the Point on the Canvas.

from [Link] import *


class Canvas(object):
"""Represents a canvas.
attributes: width, height, background color"""
a_canvas = Canvas()
a_canvas.width = 500
a_canvas.height = 500
class Point(object):
"represents a point in 2-D space"
p = Point()
p.x = 60
p.y = 15
def draw_point(canvas, point):
bbox = [[point.x, point.y], [point.x, point.y]]
drawn_canvas = [Link]([Link], [Link])
drawn_canvas.rectangle(bbox, fill="black")

world = World()
draw_point(a_canvas,p)
[Link]()
1. d. Define a new class called Circle with appropriate attributes and instantiate a few Circle
[Link] a function called draw_circle that draws circles on the canvas.

from [Link] import *


class Canvas(object):
"""Represents a canvas.
attributes: width, height, background color"""
a_canvas = Canvas()
a_canvas.width = 500
a_canvas.height = 500

class Point(object):
"represents a point in 2-D space"
p = Point()
p.x = 60
p.y = 15

class Circle(object):
"""Represents a circle.
attributes: center point, radius"""
c = Circle()
[Link] = 50
[Link] = Point()
[Link].x = 20
[Link].y = 20
def draw_circle(canvas, circle):
drawn_canvas = [Link]([Link], [Link])
drawn_canvas.circle([[Link].x, [Link].y], [Link])

world = World()
draw_circle(a_canvas,c)
[Link]()
2. Write a Python program to demonstrate the usage of Method Resolution Order (MRO) in
multiplelevels of Inheritances.

#Method Resolution Order(MRO)


#A program to understand the order of execution of methods
#in several base classes according to MRO
#Multiple inheritance with several classes
class A(object):
def method(self):
print("A class method")
super().method()
class B(object):
def method(self):
print("B class method")
super().method()
class C(object):
def method(self):
print("C class method")
class X(A,B):
def method(self):
print("X class method")
super().method()
class Y(B,C):
def method(self):
print("Y class method")
super().method()
class P(X,Y,C):
def method(self):
print("P class method")
super().method()
pobj=P()
[Link]()
3. Write a python code to read a phone number and email-id from the user and
validate it forcorrectness.
import re
#Define a function for validate mobile number
def valid_number(number):

# 1) Begins with 0 or 91
# 2) Then contains 6,7 or 8 or 9.
# 3) Then contains 9 digits
Pattern = [Link]("(0|91)?[6-9][0-9]{9}")
return [Link](number)

#Define a function for validate email address


def validate_email(email):
# regular expression for an email address
pattern = [Link](r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
# check if the pattern matches the email address
match = [Link](email)
return match

#main method
if __name__ == "__main__":

number = input("Enter your mobile number: ")


if (valid_number(number)):
print ("Valid Number")
else :
print ("Invalid Number")
email = input("Enter email address: ")
match = validate_email(email)
if match:
print("Valid email address.")
else:
print("Invalid email address.")

You might also like