Question: def find _ coordinates _ from _ file ( filename ) : x , y = 0 , 0 try: with open ( filename ,

def find_coordinates_from_file(filename):
x, y =0,0
try:
with open(filename,'r') as file:
movements = file.read().strip()
for move in movements:
if move =='N':
y +=1
elif move =='S':
y -=1
elif move =='E':
x +=1
elif move =='W':
x -=1
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return None
return (x, y)
# Test the function with the given examples
print(find_coordinates_from_file('coordinates1.txt')) # Replace 'coordinates1.txt' with your file name

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 Databases Questions!