In this lesson we show how to write modular code in micropython. We demonstrate this with a simple example of how to calculate parameters of interest for a given rectangle. For your convenience, the code developed in the video is presented below. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | def rectSolve(len,wid): #global area,perimeter,diagonal area=rectArea(len,wid) perimeter=rectPerimeter(len,wid) diagonal=rectDiagonal(len,wid) return area,perimeter,diagonal def rectArea(len,wid): area=len*wid return area def rectPerimeter(len,wid): perimeter=2*len+2*wid return perimeter def rectDiagonal(len,wid): diag=(len**2+wid**2)**(1/2) return diag while True: l=float(input("What is Length of your Rectangle? ")) w=float(input("what is the Width of your Rectuangle? ")) a,p,d = rectSolve(l,w) print("Your Rectangle Information: ") print("Area: ",a) print("Perimeter: ",p) print("Diagonal: ",d) print() |