![]() Press F7 repeatedly until you see the expression factorial(4) in the focus box. Luckily, Thonny can explain this concept intuitively without sweeping important details under the carpet.Ĭopy following recursive program into Thonny and run it in debug mode ( Ctrl+F5 or Ctrl+Shift+F5). Understanding parameter passing, local variables, returning and recursion all benefit from the notion of stack frame. Turns out the “jump model” is sufficient only with the simplest functions. Is it the whole story? Do we need to care? Traditional debuggers show us that when you step into the call, the focus jumps into the function definition (and later magically back to the original location). On the surface there is nothing complicated - you give name to a code and refer to it (call it) somewhere else in the code. Does it look like something which understands the purpose of your code or more like a dumb machine following simple rules? Function callsįunction call is a programming concept which often causes great deal of confusion to beginners. You should see a triangle made of periods appear in the shell pane.Ĭontinue pressing F7 and observe how Python moves forward with really small steps. Copy following program text into the editor and save it into a file (Ctrl+S). When first launching Thonny, it does some preparations and then presents an empty editor and the Python shell. Install it with sudo dnf install thonny or with a graphical tool of your choice (such as Software). Thonny is included in Fedora repositories since version 27. Thonny is a beginner-friendly Python IDE, developed in University of Tartu, Estonia, which takes a different approach as its debugger is designed specifically for learning and teaching programming.Īlthough Thonny is suitable for even total beginners, this post is meant for readers who have at least some experience with Python or another imperative language. Unfortunately, most debuggers are optimized for professional usage and assume the user already knows the semantics of language constructs (e.g. Programmers usually tackle this situation with a tool called a debugger, which allows running their program step-by-step. ![]() Commonly, this means you overlooked something or misunderstood a language construct, and you need to locate the place in the code where your expectations and reality diverge. Even when you finally get your colons and parentheses right, there is still a big chance that the program doesn’t do what you intended. ![]() The code in this program contain several utime.sleep(0.1) instructions to slow things down enough so that we can see what is going on.Learning to program is hard. The full code is in the demo program below and a video of the program running is on the next step. I've made a simple command to call the rather complicated routine. Thankfully, you do need to understand the maths to use it. This all sounds quite easy but it involves a great deal of mathematics. If we do not have a horizontal edge we split the original triangle into two triangles with a horizontal line and then draw the triangles separately. ![]() This is easier if we have a triangle with either a horizontal top edge or horizontal bottom edge. The method is similar to that used with filled circles - we draw horizontal lines between the edges to fill in the space. def triangle(x1,y1,x2,y2,x3,圓,c): # Draw outline triangleĭrawing a filled triangle is much more difficult. def ring(x,y,r,c):ĭrawing triangle outlines is very easy - we just draw three straight lines. If we only draw the end points on the circumference of the circle we will get a ring. def circle(x,y,r,c):Ī = int(math.sqrt(r*r-i*i)) # Pythagoras! Drawing a series of such lines, for every pixel line, will fill in the circle. If we then draw horizontal lines of length a in both directions from the vertical diameter it will touch both sides of the circle at its circumference. If we draw a radius in a circle and then draw in vertical and horizontal lines to form a right angled triangle as shown in the diagram we can use the theorem of Pythagoras to calculate the length of side a for every value of i.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |