Project - 0x0C. Python - Almost A Circle - Addis Ababa Intranet
Project - 0x0C. Python - Almost A Circle - Addis Ababa Intranet
You have a captain's log due before 2023-10-15 (in about 23 hours)! Log it now!
(/captain_logs/4174757/edit)
(/)
(/plann
(/projec
0x0C. Python - Almost a circle
(/corre
Python OOP
?(/dashb
By: Guillaume
Weight: 1
Project will start Oct 12, 2023 6:00 AM, must end by Oct 17, 2023 6:00 AM
(/dash
Checker will be released at Oct 16, 2023 6:00 PM
(/conce
Manual QA review must be done (request it when you are done with the project)
(/serve
Background Context
(https://alx-
The AirBnB project is a big part of the Higher level curriculum. This project will help you be ready for it.
(/user_
students.slack.com)
In this project, you will review everything about Python:
(https://discord.com/app)
(/dash Import
Exceptions
Class
(/users/my_profile)
Private attribute
(/user
https://intranet.alxswe.com/projects/331 1/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Getter/Setter
Class method
Static method
(/) Inheritance
Unittest
Read/Write file
(https://alx-
students.slack.com)
Resources
(https://discord.com/app)
Read or watch:
args/kwargs (/rltoken/7gc6UzxSL81HcuAwklUbuQ)
(/users/my_profile)
JSON encoder and decoder (/rltoken/rGVU9mt57rVURGnjK6n4_Q)
https://intranet.alxswe.com/projects/331 2/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
(/)
Learning Objectives
At the end of this project, you are expected to be able to explain to anyone (/rltoken/SBdRhGGBuqzWcwcuKyapSQ),
without the help of Google:
General
What is Unit testing and how to implement it in a large project
How to serialize and deserialize a Class
How to write and read a JSON file
What is *args and how to use it
What is **kwargs and how to use it
How to handle named arguments in a function
Copyright - Plagiarism
You are tasked to come up with solutions for the tasks below yourself to meet with the above learning objectives.
You will not be able to meet the objectives of this or any following project by copying and pasting someone else’s
work.
You are not allowed to publish any content of this project.
Any form of plagiarism is strictly forbidden and will result in removal from the program.
Requirements
Python Scripts
(https://alx-
Allowed editors: vi , vim , emacs
students.slack.com)
All your files will be interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.8.5)
All your files should end with a new line
(https://discord.com/app)
The first line of all your files should be exactly #!/usr/bin/python3
A README.md file, at the root of the folder of the project, is mandatory
Your code should use the pycodestyle (version 2.8.* )
(/users/my_profile)
All your files must be executable
https://intranet.alxswe.com/projects/331 3/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Tasks
0. If it's not tested it doesn't work mandatory
(https://alx-
students.slack.com)
All your files, classes and methods must be unit tested and be PEP 8 validated.
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 4/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
OK
guillaume@ubuntu:~/$
Note that this is just an example. The number of tests you create can be different from the above example.
Repo:
Create a folder named models with an empty file __init__.py inside - with this file, the folder will become a Python
package
(/users/my_profile)
otherwise, increment __nb_objects and assign the new value to the public instance attribute id
https://intranet.alxswe.com/projects/331 5/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
This class will be the “base” of all other classes in this project. The goal of it is to manage id attribute in all your future
classes and to avoid duplicating the same code (by extension, same bugs)
if __name__ == "__main__":
b1 = Base()
print(b1.id)
b2 = Base()
print(b2.id)
b3 = Base()
print(b3.id)
b4 = Base(12)
print(b4.id)
b5 = Base()
print(b5.id)
guillaume@ubuntu:~/$ ./0-main.py
1
2
3
12
4
(https://alx-
guillaume@ubuntu:~/$
students.slack.com)
(https://discord.com/app)
Repo:
Why private attributes with getter/setter? Why not directly public attribute?
Because we want to protect attributes of our class. With a setter, you are able to validate what a developer is trying to
assign to a variable. So after, in your class you can “trust” these attributes.
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 7/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1 = Rectangle(10, 2)
print(r1.id)
r2 = Rectangle(2, 10)
print(r2.id)
r3 = Rectangle(10, 2, 0, 0, 12)
print(r3.id)
guillaume@ubuntu:~/$ ./1-main.py
1
2
12
guillaume@ubuntu:~/$
Repo:
(https://alx-
Done? Help
students.slack.com)
3. Validate attributes mandatory
(https://discord.com/app)
Update the class Rectangle by adding validation of all setter methods and instantiation ( id excluded):
If the input is not an integer, raise the TypeError exception with the message: <name of the attribute> must be
(/users/my_profile)
an integer . Example: width must be an integer
https://intranet.alxswe.com/projects/331 8/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
If width or height is under or equals 0, raise the ValueError exception with the message: <name of the
attribute> must be > 0 . Example: width must be > 0
If x or y is under 0, raise the ValueError exception with the message: <name of the attribute> must be >= 0 .
(/) Example: x must be >= 0
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 9/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
try:
Rectangle(10, "2")
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
r = Rectangle(10, 2)
r.width = -10
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
r = Rectangle(10, 2)
r.x = {}
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
try:
Rectangle(10, 2, 3, -1)
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
guillaume@ubuntu:~/$ ./2-main.py
[TypeError] height must be an integer
(https://alx-
[ValueError] width must be > 0
students.slack.com)
[TypeError] x must be an integer
[ValueError] y must be >= 0
guillaume@ubuntu:~/$
(https://discord.com/app)
(/users/my_profile)
Repo:
https://intranet.alxswe.com/projects/331 10/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Done? Help
Update the class Rectangle by adding the public method def area(self): that returns the area value of the Rectangle
instance.
if __name__ == "__main__":
r1 = Rectangle(3, 2)
print(r1.area())
r2 = Rectangle(2, 10)
print(r2.area())
r3 = Rectangle(8, 7, 0, 0, 12)
print(r3.area())
guillaume@ubuntu:~/$ ./3-main.py
6
(https://alx-
20
students.slack.com)
56
guillaume@ubuntu:~/$
(https://discord.com/app)
Repo:
(/users/my_profile)
GitHub repository: alx-higher_level_programming
https://intranet.alxswe.com/projects/331 11/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Directory: 0x0C-python-almost_a_circle
File: models/rectangle.py
(/)
Done? Help
5. Display #0 mandatory
Update the class Rectangle by adding the public method def display(self): that prints in stdout the Rectangle
instance with the character # - you don’t need to handle x and y here.
if __name__ == "__main__":
r1 = Rectangle(4, 6)
r1.display()
print("---")
r1 = Rectangle(2, 2)
r1.display()
guillaume@ubuntu:~/$ ./4-main.py
####
####
####
(https://alx-
####
students.slack.com)
####
####
---
(https://discord.com/app)
##
##
guillaume@ubuntu:~/$
(/users/my_profile)
https://intranet.alxswe.com/projects/331 12/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Repo:
Done? Help
6. __str__ mandatory
Update the class Rectangle by overriding the __str__ method so that it returns [Rectangle] (<id>) <x>/<y> -
<width>/<height>
if __name__ == "__main__":
r1 = Rectangle(4, 6, 2, 1, 12)
print(r1)
r2 = Rectangle(5, 5, 1)
print(r2)
guillaume@ubuntu:~/$ ./5-main.py
[Rectangle] (12) 2/1 - 4/6
(https://alx-
[Rectangle] (1) 1/0 - 5/5
students.slack.com)
guillaume@ubuntu:~/$
(https://discord.com/app)
Repo:
File: models/rectangle.py
7. Display #1 mandatory
Update the class Rectangle by improving the public method def display(self): to print in stdout the Rectangle
instance with the character # by taking care of x and y
if __name__ == "__main__":
r1 = Rectangle(2, 3, 2, 2)
r1.display()
print("---")
r2 = Rectangle(3, 2, 1, 0)
r2.display()
(/users/my_profile)
Repo:
https://intranet.alxswe.com/projects/331 14/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Done? Help
8. Update #0 mandatory
Update the class Rectangle by adding the public method def update(self, *args): that assigns an argument to each
attribute:
This type of argument is called a “no-keyword argument” - Argument order is super important.
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 15/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1.update(89)
print(r1)
r1.update(89, 2)
print(r1)
r1.update(89, 2, 3)
print(r1)
r1.update(89, 2, 3, 4)
print(r1)
r1.update(89, 2, 3, 4, 5)
print(r1)
guillaume@ubuntu:~/$ ./7-main.py
[Rectangle] (1) 10/10 - 10/10
[Rectangle] (89) 10/10 - 10/10
[Rectangle] (89) 10/10 - 2/10
[Rectangle] (89) 10/10 - 2/3
[Rectangle] (89) 4/10 - 2/3
(https://alx-
[Rectangle] (89) 4/5 - 2/3
students.slack.com)
guillaume@ubuntu:~/$
(https://discord.com/app)
Repo:
(/users/my_profile)
GitHub repository: alx-higher_level_programming
Directory: 0x0C-python-almost_a_circle
https://intranet.alxswe.com/projects/331 16/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
File: models/rectangle.py
9. Update #1 mandatory
Update the class Rectangle by updating the public method def update(self, *args): by changing the prototype to
update(self, *args, **kwargs) that assigns a key/value argument to attributes:
This type of argument is called a “key-worded argument”. Argument order is not important.
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 17/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1.update(height=1)
print(r1)
r1.update(width=1, x=2)
print(r1)
guillaume@ubuntu:~/$ ./8-main.py
[Rectangle] (1) 10/10 - 10/10
[Rectangle] (1) 10/10 - 10/1
[Rectangle] (1) 2/10 - 1/1
[Rectangle] (89) 3/1 - 2/1
[Rectangle] (89) 1/3 - 4/2
guillaume@ubuntu:~/$
(https://alx-
students.slack.com)
Repo:
(/users/my_profile)
https://intranet.alxswe.com/projects/331 18/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Done? Help
As you know, a Square is a special Rectangle, so it makes sense this class Square inherits from Rectangle. Now you have a
Square class who has the same attributes and same methods.
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 19/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
s1 = Square(5)
print(s1)
print(s1.area())
s1.display()
print("---")
s2 = Square(2, 2)
print(s2)
print(s2.area())
s2.display()
print("---")
s3 = Square(3, 1, 3)
print(s3)
print(s3.area())
s3.display()
guillaume@ubuntu:~/$ ./9-main.py
[Square] (1) 0/0 - 5
25
#####
#####
(https://alx-
#####
students.slack.com)
#####
#####
---
(https://discord.com/app)
[Square] (2) 2/0 - 2
4
(/users/my_profile)
##
##
https://intranet.alxswe.com/projects/331 20/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
---
[Square] (3) 1/3 - 3
9
(/)
###
###
###
guillaume@ubuntu:~/$
Repo:
Done? Help
Update the class Square by adding the public getter and setter size
The setter should assign (in this order) the width and the height - with the same value
The setter should have the same value validation as the Rectangle for width and height - No need to change the
exception error message (It should be the one from width )
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 21/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
s1 = Square(5)
print(s1)
print(s1.size)
s1.size = 10
print(s1)
try:
s1.size = "9"
except Exception as e:
print("[{}] {}".format(e.__class__.__name__, e))
guillaume@ubuntu:~/$ ./10-main.py
[Square] (1) 0/0 - 5
5
[Square] (1) 0/0 - 10
[TypeError] width must be an integer
guillaume@ubuntu:~/$
Repo:
Done? Help
(https://discord.com/app)
12. Square update
(/users/my_profile)
mandatory
Update the class Square by adding the public method def update(self, *args, **kwargs) that assigns attributes:
https://intranet.alxswe.com/projects/331 22/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 23/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
s1 = Square(5)
print(s1)
s1.update(10)
print(s1)
s1.update(1, 2)
print(s1)
s1.update(1, 2, 3)
print(s1)
s1.update(1, 2, 3, 4)
print(s1)
s1.update(x=12)
print(s1)
s1.update(size=7, y=1)
print(s1)
https://intranet.alxswe.com/projects/331 24/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
[Square] (89) 12/1 - 7
guillaume@ubuntu:~/$
(/)
Repo:
Done? Help
Update the class Rectangle by adding the public method def to_dictionary(self): that returns the dictionary
representation of a Rectangle :
id
width
height
x
y
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 25/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1 = Rectangle(10, 2, 1, 9)
print(r1)
r1_dictionary = r1.to_dictionary()
print(r1_dictionary)
print(type(r1_dictionary))
r2 = Rectangle(1, 1)
print(r2)
r2.update(**r1_dictionary)
print(r2)
print(r1 == r2)
guillaume@ubuntu:~/$ ./12-main.py
[Rectangle] (1) 1/9 - 10/2
{'x': 1, 'y': 9, 'id': 1, 'height': 2, 'width': 10}
<class 'dict'>
[Rectangle] (2) 0/0 - 1/1
[Rectangle] (1) 1/9 - 10/2
False
guillaume@ubuntu:~/$
Repo:
(https://alx-
GitHub repository: alx-higher_level_programming
students.slack.com)
Directory: 0x0C-python-almost_a_circle
File: models/rectangle.py
(https://discord.com/app)
Done?
(/users/my_profile)
Help
https://intranet.alxswe.com/projects/331 26/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
(/) Update the class Square by adding the public method def to_dictionary(self): that returns the dictionary
representation of a Square :
id
size
x
y
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 27/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
s1 = Square(10, 2, 1)
print(s1)
s1_dictionary = s1.to_dictionary()
print(s1_dictionary)
print(type(s1_dictionary))
s2 = Square(1, 1)
print(s2)
s2.update(**s1_dictionary)
print(s2)
print(s1 == s2)
guillaume@ubuntu:~/$ ./13-main.py
[Square] (1) 2/1 - 10
{'id': 1, 'x': 2, 'size': 10, 'y': 1}
<class 'dict'>
[Square] (2) 1/0 - 1
[Square] (1) 2/1 - 10
False
guillaume@ubuntu:~/$
Repo:
(https://alx-
GitHub repository: alx-higher_level_programming
students.slack.com)
Directory: 0x0C-python-almost_a_circle
File: models/square.py
(https://discord.com/app)
Done?
(/users/my_profile)
Help
https://intranet.alxswe.com/projects/331 28/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
(/) JSON is one of the standard formats for sharing data representation.
Update the class Base by adding the static method def to_json_string(list_dictionaries): that returns the JSON
string representation of list_dictionaries :
if __name__ == "__main__":
r1 = Rectangle(10, 7, 2, 8)
dictionary = r1.to_dictionary()
json_dictionary = Base.to_json_string([dictionary])
print(dictionary)
print(type(dictionary))
print(json_dictionary)
print(type(json_dictionary))
guillaume@ubuntu:~/$ ./14-main.py
{'x': 2, 'width': 10, 'id': 1, 'height': 7, 'y': 8}
<class 'dict'>
[{"x": 2, "width": 10, "id": 1, "height": 7, "y": 8}]
(https://alx-
<class 'str'>
students.slack.com)
guillaume@ubuntu:~/$
(https://discord.com/app)
Repo:
(/users/my_profile)
GitHub repository: alx-higher_level_programming
Directory: 0x0C-python-almost_a_circle
https://intranet.alxswe.com/projects/331 29/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
File: models/base.py
Update the class Base by adding the class method def save_to_file(cls, list_objs): that writes the JSON string
representation of list_objs to a file:
list_objs is a list of instances who inherits of Base - example: list of Rectangle or list of Square instances
If list_objs is None , save an empty list
The filename must be: <Class name>.json - example: Rectangle.json
You must use the static method to_json_string (created before)
You must overwrite the file if it already exists
if __name__ == "__main__":
r1 = Rectangle(10, 7, 2, 8)
r2 = Rectangle(2, 4)
Rectangle.save_to_file([r1, r2])
(https://alx-
guillaume@ubuntu:~/$ ./15-main.py
students.slack.com)
[{"y": 8, "x": 2, "id": 1, "width": 10, "height": 7}, {"y": 0, "x": 0, "id": 2, "width": 2, "height":
4}]
guillaume@ubuntu:~/$
(https://discord.com/app)
(/users/my_profile)
Repo:
https://intranet.alxswe.com/projects/331 30/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Done? Help
Update the class Base by adding the static method def from_json_string(json_string): that returns the list of the
JSON string representation json_string :
if __name__ == "__main__":
list_input = [
{'id': 89, 'width': 10, 'height': 4},
{'id': 7, 'width': 1, 'height': 7}
]
json_list_input = Rectangle.to_json_string(list_input)
list_output = Rectangle.from_json_string(json_list_input)
print("[{}] {}".format(type(list_input), list_input))
(https://alx-
print("[{}] {}".format(type(json_list_input), json_list_input))
students.slack.com)
print("[{}] {}".format(type(list_output), list_output))
guillaume@ubuntu:~/$ ./16-main.py
(https://discord.com/app)
[<class 'list'>] [{'height': 4, 'width': 10, 'id': 89}, {'height': 7, 'width': 1, 'id': 7}]
[<class 'str'>] [{"height": 4, "width": 10, "id": 89}, {"height": 7, "width": 1, "id": 7}]
[<class 'list'>] [{'height': 4, 'width': 10, 'id': 89}, {'height': 7, 'width': 1, 'id': 7}]
(/users/my_profile)
guillaume@ubuntu:~/$
https://intranet.alxswe.com/projects/331 31/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Repo:
Done? Help
Update the class Base by adding the class method def create(cls, **dictionary): that returns an instance with all
attributes already set:
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 32/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1 = Rectangle(3, 5, 1)
r1_dictionary = r1.to_dictionary()
r2 = Rectangle.create(**r1_dictionary)
print(r1)
print(r2)
print(r1 is r2)
print(r1 == r2)
guillaume@ubuntu:~/$ ./17-main.py
[Rectangle] (1) 1/0 - 3/5
[Rectangle] (1) 1/0 - 3/5
False
False
guillaume@ubuntu:~/$
Repo:
(https://alx-
Done? Help
students.slack.com)
19. File to instances mandatory
(https://discord.com/app)
Update the class Base by adding the class method def load_from_file(cls): that returns a list of instances:
https://intranet.alxswe.com/projects/331 33/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Otherwise, return a list of instances - the type of these instances depends on cls (current class using this method)
You must use the from_json_string and create methods (implemented previously)
(/)
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 34/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
if __name__ == "__main__":
r1 = Rectangle(10, 7, 2, 8)
r2 = Rectangle(2, 4)
list_rectangles_input = [r1, r2]
Rectangle.save_to_file(list_rectangles_input)
list_rectangles_output = Rectangle.load_from_file()
print("---")
print("---")
print("---")
s1 = Square(5)
s2 = Square(7, 9, 1)
list_squares_input = [s1, s2]
(https://alx-
Square.save_to_file(list_squares_input)
students.slack.com)
list_squares_output = Square.load_from_file()
(https://discord.com/app)
for square in list_squares_input:
print("[{}] {}".format(id(square), square))
(/users/my_profile)
print("---")
https://intranet.alxswe.com/projects/331 35/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
Repo:
Done? Help
(https://alx-
students.slack.com)
(https://discord.com/app) Done with the mandatory tasks? Unlock 2 advanced tasks now!
(/users/my_profile)
https://intranet.alxswe.com/projects/331 36/37
10/14/23, 10:18 PM Project: 0x0C. Python - Almost a circle | Addis Ababa Intranet
(/)
(https://alx-
students.slack.com)
(https://discord.com/app)
(/users/my_profile)
https://intranet.alxswe.com/projects/331 37/37