Ad

How Can I Make These Shapes Different In Loop With Python?

- 1 answer

So I have this code here, everything is good but it has to make 20 different shapes, at the moment it makes 20 same shapes. Can somebody help me plz?

import random from turtle import*

def func(lenght, nr_of_sides): i = 0 while i in range(20): i += 1 for element in range(nr_of_sides): forward(lenght) angle = 360/nr_of_sides left(angle)

a = random.randint(1, 100) b = random.randint(3, 10) func(a, b)

exitonclick()

Ad

Answer

I added the for loop out of the function so every time it starts a new iteration the a and b values are random.

import random 
from turtle import*

def func(lenght, nr_of_sides):
    for element in range(nr_of_sides): 
        forward(lenght) 
        angle = 360/nr_of_sides
        left(angle)


for i in range(0, 20):
    a = random.randint(1, 100) 
    b = random.randint(3, 10) 
    func(a, b)

exitonclick()

Result:

result

Ad
source: stackoverflow.com
Ad