Home > Software design >  How can i make these shapes different in loop with python?
How can i make these shapes different in loop with python?

Time:02-12

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()

CodePudding user response:

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

CodePudding user response:

Well you're creating a and b as random values and you're passing them to your function. But once passed, the values won't change. Try putting the random.randint bits inside your while loop to have it generate a new random value with each iteration of the loop.

  • Related