[Tutor] Need help please

Alan Gauld alan.gauld at yahoo.co.uk
Mon Apr 16 03:38:48 EDT 2018


On 16/04/18 03:30, Sandra Sherif via Tutor wrote:
> Dear Python Tutor,
> 
> I am in desperate need for help with programming on python. I am new to using python and I’m trying to write a program called “turtle tag”. I’m trying to do these things in the program: 
> a. Asks how many turtles are playing tag
> b. Creates a turtle, assigns it a random color, and assigns it a random starting
> position in the graphical window
> c. Randomly assigns one of the turtles to be “it”, and marking that turtle in some
> way (like making it a special color no other turtle is allowed to be)
> d. Figures out which turtle is the closest to the turtle that is “it”
> e. Moves the turtle that is it to a point where it is touching the closest other turtle
> 
> Can you please show me how it should be written please? Thank you so much.

When writing almost any program it helps to divide it
into chunks that you know how to do. You can then build
each chunk separate and gradually combine them to form
the final complete program.

Your problem has already been divided into chunks: a-e.
So which bits of that can you do? And which bits puzzle you?

a) Do you know how to get a number from a user?

b1) Can you create a turtle?
b2) Can you choose a random color value?
b3) Can you set a turtles color?
b4) Can you choose a random starting position?
b5) Can you set a turtles position?

c1) Can you create several turtles?
c2) Can you store them in a list?
c3) Can you choose a random list element?
c4) can you store a reference to a list element?
c5) Can you choose a color that no other turtle can take?
    (This may mean changing the way b2 works)

d1) Can you work out the distance between two turtles?
d2) Can you repeat that for each turtle in your list?

e) Can you move a turtle to a new location?

For each item that you know how to do write a small
function (do you know how to write functions?) named
after the action it performs.
For example:
getNumber(),
createTurtle(),
getRandomColor(),
setColor(turtle, color) etc...

Test each function. Remember that functions that
get a value will need to have that value stored
in a variable.

Write another function that uses these function to
complete each major step (a-e) above.
eg initializeTurtle() could cover steps a and b.

For the steps you don't know how to do, or those that
don't work, come back here for help.

Show us your code plus any error messages(in full)

Hint: It may make life easier if you forget about the
random aspects for now and get it working with fixed
values - easier to debug... Once it works with fixed
values introduce randomness bit by bit - say colors
first then position. Divide and conquer is the key.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list