Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first time...help
#11
Any advance please??
Reply
#12
Figure 1? Where is it?
Reply
#13
...the figure 1 was just an example...
It's a tree built with three triangles in the middle of the screen.
Reply
#14
Guys I have the same problem...but I need a tree with three different triangle with length 10, 20 and 30 respectively.
Reply
#15
Show some code, so we have an idea how to help.
Reply
#16
JJgo's last post on this thread was originally at https://python-forum.io/Thread-making-a-basic-tree-help, then was split out, and I merged it into this existing thread. It looks like these are two students from the same class.
Reply
#17
You already know how to do loops, you just do not realize it. All loops do, is repeat a linear task or some kind of pattern.

For example your code to create a triangle is a loop:
for sides in range(1, 4) :
    forward(10)
    left(120)
It could be written instead as, but the loop is better:
forward(10)
left(120)
forward(10)
left(120)
forward(10)
left(120)
When doing graphics, it is often advantageous to have known starting and ending points that are convenient, and the same starting and ending orientation. Your code works perfectly fine, but starts and ends at the lower right corner of the triangle.

Instead of starting at the lower right corner, I prefer to start at the Apex. For example:
    # Draw the Current Triangle (starting and ending at the Apex)
    # Original Orientation was heading 0
    # Reset Orientation to heading 0 when done
    penup()
    left(120)
    pendown()
    for sides in range(1, 4) :
        left(120)
        forward(10)
    penup()
    setheading(0)
Now we want to repeat the above triangle for 3 different triangle sizes. One way to do it is with an if..elif..else construction. It is probably better to do it with a list. To create 3 different sizes we could do something like:
my_list = [60, 80, 100]

number_of_triangles = len(my_list)  
    
# NOTE: Triangle number in for loop starts at ZERO
for triangle_number in range(number_of_triangles):

    # Get the Pixel Length of the Triangle
    pixel_length = my_list[triangle_number]
    print(triangle_number + 1, pixel_length)
It is your job to try to combine the two constructions. If you have problems, please post all your code, and tell us what the problem is.

If you are successful, and you have time, you should try:
a. Modify the code to look like your original output with the apex of the next shape at the base of the first shape.
Hint: If you end at the apex, to start the next shape at the base, you will need to calculate the height of the triangle.
b. Put the 'triangle code' in a function, if and when you have learned functions.
c. Create functions for other shapes, e.g. square, pentagon.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020