You are Here: Home

Announcements Posted on
Why bother with a post grad? Are they even worth it? Have your say! 26-10-2016
1. I am currently, learning Charles Dierbach's book, Introduction to Computer Science using Python.

I am trying to make my turtle bounce off the canvas but it does not work. I have tried different variations but have not being able to fix it

Here is my code:

Code:
```
Code:
#Drunkard walk PYTHON

from turtle import *
from random import *

#draw a house
def house(t):
pu()
goto(270,100)
pd()
pensize(5)
for i in range(4):
fd(100)
right(90)
fd(100)
fd(100)
pu()
goto(200,0)
pd()
fd(70)
fd(40)
fd(70)
pu()

pu()
goto(80, 280)
pd()
begin_fill()
color('black')
fd(250)
fd(100)
fd(250)
fd(100)
end_fill()
pu()
goto(80,25)
pd()
begin_fill()
color('white')
for i in range(4):
fd(70)
right(90)
fd(100)
right(90)
end_fill()
pu()
goto(80, -45)
pd()
begin_fill()
color('black')
fd(240)
fd(100)
fd(240)
fd(100)
end_fill()

#this is my code to keep turtle on canvas
def isInScreen(window, t):
xmin=-299
xmax=299
ymin=-299
ymax=299

xTcor = t.xcor()
yTcor = t.ycor()

if xTcor<xmin or xTcor>xmax:
if yTcor<ymin or yTcor>ymax:
#house coord
if (170<=xTcor<=200 or 200<=xTcor<=270)and yTcor==0:
if xTcor==170 and 0<=yTcor<=100:
if (170<=xTcor<=200 or 200<=xTcor<=270) and yTcor==100:
if xTcor==270 and 0<=yTcor<=100:
if 170<=xTcor<=271 and 100<=yTcor<=150:
if  200<=xTcor<=240 and yTcor ==0:
return  100

#################MAIN####################
setup(600,600)
window=Screen()
window.title("Drunkard walk")
window.bgcolor("grey")

#get the turtle and change the shape
t=getturtle()
t.shape('turtle')
shapesize(2,1.2,1.2)

pu()

#change coords and make the ouer roads
goto(290,290)
pd()
pensize(10)

for i in range(4):
fd(580)
right(90)

shape('circle')
house(t)
goto(80,0)
penup()
goto(-250,-260)
shapesize(1,1,1)
walking = True
while walking:
pendown()
fd(10)
color = choice(["black", "red", "yellow", "blue", "white", "green"])
fillcolor(color)
ch = randrange(2)
if ch == 0:
left(90)
else:
right(90)

mainloop()
exitonclick()
```
Please help me how to fix it and where i went wrong?

Register

Thanks for posting! You just need to create an account in order to submit the post
1. this can't be left blank
2. this can't be left blank
3. this can't be left blank

6 characters or longer with both numbers and letters is safer

4. this can't be left empty
your full birthday is required
1. Oops, you need to agree to our Ts&Cs to register

Updated: July 24, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Today on TSR

Who is getting a uni offer this half term?

Find out which unis are hot off the mark here

Poll
Useful resources

Make your revision easier

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

Groups associated with this forum:

View associated groups
Study resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.