Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle and Random
#1
So it keeps erroring a the the second random (rnumber = random.randint(1,4) number i'm trying to generate. any ideas why?
#Program that uses the turtle and random functions to create a drunk turtle

#import the turtle
import turtle
#import random
import random

#set random move values
random = random.randint(50,100)
#generate random direction

rnumber = random.randint(1,4)

if rnumber == 1:
    turtle.heading = 0
elif rnumber == 2:
    turtle.heading = 90
elif rnumber == 3:
    turtle.heading = 180
elif rnumber == 4:
    turtle.heading = 270

#change turtle into a turtle
turtle.shape("turtle")
#startr drawing
turtle.pd()
#turn a random direction 1
turtle.setheading(rnumber)
#forward a random amount 1
turtle.fd(random)
#turn a random direction 2
turtle.setheading(rnumber)
#forward a random amount 2
turtle.fd(random)
#turn a random direction 3
turtle.setheading(rnumber)
#forward a random amount 3
turtle.fd(random)
#turn a random direction 4
turtle.setheading(rnumber)
#forward a random amount 4
turtle.fd(random)
#turn a random direction 5
turtle.setheading(rnumber)
#forward a random amount 5
turtle.fd(random)
#turn a random direction 6
turtle.setheading(rnumber)
#forward a random amount 6
turtle.fd(random)
#turn a random direction 7
turtle.setheading(rnumber)
#forward a random amount 7
turtle.fd(random)
#turn a random direction 8
turtle.setheading(rnumber)
#forward a random amount 8
turtle.fd(random)
#turn a random direction 9
turtle.setheading(rnumber)
#forward a random amount 9
turtle.fd(random)
#turn a random direction 10
turtle.setheading(rnumber)
#forward a random amount 10
turtle.fd(random)

turtle.pu()
turtle.home()
turtle.right(90)
turtle.fd(20)
turtle.write("Total distance covered by turtle ="+str(turtle.distance))
turtle.fd(30)
turtle.write("Final position of turtle: x= "+str(turtle.xcor())+str(" y= ")+str(turtle.ycor()))
Reply
#2
Because you broke the link the random module. By naming your first random number 'random', you made it so the name 'random' no longer refers to a module with randint in it. Be careful with your variable names.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
makes sense! thanks so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,497 Mar-05-2018, 03:48 PM
Last Post: mpd

Forum Jump:

User Panel Messages

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