473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why does this relatively simple code keep giving me errors?!?!?

Okay, so here is the exercise I'm trying to complete. Create a turtle object in turtle graphics in a coin field, with each coin an object itself. Make the turtle randomly move while simultaneously collecting/eating coins(in my while loop, I used the random generator). As the turtle collects a coin, the turtle gets bigger and the coins should dissapear from the screen(also in my while loop, I made the shapesize increase,the coin it contacted white). When the turtle leaves the coin field boundaries, the turtle should stop and a score should be printed (in my while loop, I have two conditionals for this and I increased the score in the while loop).

The problem is, I keep getting errors, that I don't understand, which means I don't know if my code works or not. So can you help me by spotting the error(s) and seeing if my code works? Thanks.
Expand|Select|Wrap|Line Numbers
  1. from turtle import *
  2. from random import *
  3.  
  4. s = Screen()
  5. s.setup(560,560)
  6. s.title("A turtle collecting coins!!")
  7.  
  8. s.tracer(False)
  9. writer = Turtle(visible = False)
  10. writer.penup()
  11. writer.goto(0, -275)
  12.  
  13. coins = []
  14. for i in range(-4,5):
  15.   for j in range(-4, 5):
  16.       c = Turtle(shape="circle")
  17.       c.color("", "orange")
  18.       c.shapesize(0.5)
  19.       c.goto(40*i, 40*j)
  20.       coins.append(c)
  21. s.tracer(True)
  22.  
  23. Molly=Turtle(shape="turtle")
  24. Molly.color("","green")
  25. Molly.shapesize(1)
  26. Molly.goto(-175,-175)
  27.  
  28. score=0
  29. while True:
  30.   Molly.fd(randint(10,30))
  31.   if Molly.distance(c)<15:
  32.     c.color("","white")
  33.     Molly.shapesize+=1
  34.     score+=1
  35.   Molly.rt(randint(0,360))
  36.   if Molly.distance(c)<15:
  37.     c.color("","white")
  38.     Molly.shapesize+=1
  39.     score+=1
  40.   Molly.bk(randint(10,30))
  41.   if Molly.distance(c)<15:
  42.     c.color("","white")
  43.     Molly.shapesize+=1
  44.     score+=1
  45.   Molly.bk(randint(10,30))
  46.   if Molly.distance(c)<15:
  47.     c.color("","white")
  48.     Molly.shapesize+=1
  49.     score+=1
  50.   Molly.lt(randint(0,360))
  51.   if Molly.distance(c)<15:
  52.     c.color("","white")
  53.     Molly.shapesize+=1
  54.     score+=1
  55.  
  56.   if Molly.xcor()<-175 or >175:
  57.     write("Score:"+str(score),align="center",font=("Courier",14,bold))
  58.     break
  59.  
  60.   if Molly.ycor()<-175 or >175:
  61.     write("Score:"+str(score),align="center",font=("Courier",14,bold))
  62.     break
Jan 3 '10 #1
2 1580
Glenton
391 Expert 256MB
Comments in your code would help you and us a lot!

However, in your loop you have "if Molly.distance(c)<15". This doesn't make sense to me. Surely c is just the last coin you've generated! So you're only checking against one of your coin!

I'd also be inclined to randomise the direction too and save some lines of code, rather than go round in circles. The problem with the current approach is that you don't check whether the turtle is out of bounds after each move. Only after each fourth move.

Another issue may be that you do the move, and then check whether it's out of bounds. That could certainly give an error, but I'm not too familiar with turtle.

Perhaps you could tell us what errors you get too?

A good way to debug is to step through the code. Or, failing that, copy it line by line into the python prompt and see what each line does (except the while loop line).

Debugging is about as important as writing the code and probably harder to do! So it's worth getting good at it on this kind of simple program...

Good luck, and let us know how it goes...
Jan 3 '10 #2
Hi,

there were errors in you code

# if Molly.xcor()<-175 or >175:
# write("Score:"+str(score),align="center",font=("Co urier",14,bold))
# break
#
# if Molly.ycor()<-175 or >175:
# write("Score:"+str(score),align="center",font=("Co urier",14,bold))
# break

see the correction
# if (Molly.xcor() < -175) or (Molly.ycor() >175):
# write("Score:"+str(score),align="center",font=("Co urier",14,bold))
# break
#
# if (Molly.ycor() < -175) or (Molly.ycor()>175):
# write("Score:"+str(score),align="center",font=("Co urier",14,bold))
# break
Jan 8 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
10
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I...
11
by: Brian W | last post by:
Yet another editor problem To reproduce do the following 1) Open a Webform and switch to HTML edit mode 2) Enter the Following (include spaces) This is some text before <asp:hyperlink...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
10
by: Tom Cole | last post by:
While I've done javascript work for as long as I can remember (since Netscape first released it), I've only ever used it for trivial things, change a color here, validate a text element there, blah...
4
by: john_smith_nebraska | last post by:
I am using VISTA . I have IIS enabled . I installed VS 2005 successfully. Under old ASPX you knwo you go to inetpub wwwroot and run a test default.aspx file to see if it executes properly....
11
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I know I sound like a one-note Johnny on this but I'm still looking for a solution. I need to display characters coming in from a serial port or a socket. I also need to be able to type...
1
by: jabbari | last post by:
Hello, Please Help us...! I have a big problem ,so i searched on google and other search engine ,then I realized that so many other people have the same problem and they, all, have'nt been able to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.