1,105,644 Community Members

Turtle Python - Random Generation, Help with Code

Member Avatar
yellowkaiq
Newbie Poster
10 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I can't get this to generate random shapes, etc. Basically what I need to do is randomly generate similar lists consisting of:
combinations of 'square', 'triangle', 'circle' and 'left'.
And random numbers from 1 to 10 for physical dimensions and 10 to 90 for angles
Have the program use 'split' to divide the numbers from the commands so that the turtles can have more variety in their random creations.

Any help would be greatly appreciated, I think I've reached a roadblock :p

import turtle
import random
import math
import time
my_screen = turtle.Screen()
my_turtle = turtle.Turtle()
turtle_commands = ['square 20','circle 20','triangle 20', \
                   'left 15 40','left 70 40',\
                   'left 90 40', 'straight 50']
number = int(input('Input a random positive number: '))

def get_random_turtle_commands(number):
    new_list = []
    i = 0
    ##for i in number:
    while(i < number):
        i=i+1
        index=math.ceil(random.random()*len(turtle_commands)-1)
        new_list=index.append(turtle_commands[index])
    return(new_list)

def draw_square(side):
    for number in range(4):
        turtle.fd(side)
        turtle.left(90)

def draw_triangle(side):
    for number in range(3):
        turtle.fd(side)
        turtle.left(60)

def draw_movement(degree,length):
    turtle.left(degree)
    turtle.fd(length)

def turtle_command(string):
    turtle.pd()
    if string == 'square 20':
        draw_square(20)
    elif string == 'circle 20':
        turtle.circle(20)
    elif string == 'triangle 20':
        draw_triangle(20)
    elif string == 'left 15 40':
        draw_movement(15, 40)
    elif string == 'left 70 40':
        draw_movement(70,40)
    elif string == 'left 90 40':
        draw_movement(90,40)
    elif string == 'straight 50':
        turtle.fd(50)
    turtle.pu()

def final_output():
    for x in string:
        turtle_command(string)
    
    
def main():
    get_random_turtle_commands(number)
    final_output()

main()
Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

You are not using split().

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

It does not appear that you are using physical dimensions, since there are no numbers < 11, but this will print 5 random combinations.

import random

shapes = ['square', 'triangle', 'circle', 'left']
physical = range(1, 11)
angles = range(10, 91, 10)
print "    shape   phy angle"
for j in range(5):
    print "%10s  %2d  %2d" % (random.choice(shapes), \
         random.choice(physical), random.choice(angles))
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: