Question: Python coding file with points: points 3 . txt has only two lines ( one for x - coordinates and one for y - coordinates

Python coding
file with points: points3.txt has only two lines (one for x-coordinates and one for y-coordinates), connect the dots given by this file. Read the x and y coordinates of points from the file points3.txt into the lists x and y. Create a list of Point objects from those coordinates. you need to append them all using a for-loop. Then, create a Polygon object giving this list of points as an input to the Polygon() function. Create a Graph Window to draw a polygon in.
Make the window size 500 by 500 pixels and set its coordinates to go from (0,0) to (800,800). Make sure to add connecting the dots to the window title.
Set the window background to lightyellow.
Draw your polygon in the window. Set the polygons fill color to lightblue.
Finish with win.getMouse() and win.close(), so the window closes after the click.
my code so far, please help me with what I am missing. No polygon is drawn but all my points print.
from graphics import *
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def create_points(x, y):
return [Point(x[i], y[i]) for i in range(len(x))]
def read_coordinates(file_name):
try:
with open(file_name, 'r') as infile:
x =[float(x_str) for x_str in infile.readline().strip().split(',')]
y =[float(y_str) for y_str in infile.readline().strip().split(',')]
return x, y
except FileNotFoundError:
print(f"Error: File '{file_name}' not found.")
return [],[] # Return empty lists to handle the error gracefully
def main():
file_name = "points3.txt"
x, y = read_coordinates(file_name)
if not x or not y:
return # Exit if there was an error reading the file
points_list = create_points(x, y)
polygon = Polygon(*points_list)
polygon.setFill('lightblue')
win = GraphWin("Connecting Dots - r",500,500)
win.setCoords(0,0,800,800)
win.setBackground("lightyellow")
polygon.draw(win)
win.getMouse()
win.close()
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!