473,806 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

8 New Member
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 1601
Glenton
391 Recognized Expert Contributor
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
dbenarfa
1 New Member
Hi,

there were errors in you code

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

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

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

Similar topics

2
14171
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 Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
383
12284
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 poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
10
9931
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 want to obtain is not possible, but I would like the advice of more experienced python programmers. I am writing a relatively complex program in python that has now around 40 files.
11
3236
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 id="hl1" runat="server " navigateurl="http://www.microsoft.com">This is my link</asp:hyperlink> And this is my text after the Hyperlink
14
4867
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 can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
10
2680
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 blah blah. With Ajax (actually, the uncovering of the XmlHTTPRequest object) I absolutely see the benefit of moving more of the UI work to the client, rather than doing page refreshes. I know there are a bunch of libraries out there...
4
2564
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. I pulled a starter program from Microsoft site and pasted it into VS 2005 at the default.aspx page of a new wesite called website1. I made sure website1 was added into IIS and was running.
11
7656
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 characters into the display myself - but that's not the main issue at this time. I've tried a scrolling multiline text box but once the original viewable area fills up and it starts scrolling the flashing of the entire area drives me nuts. The...
1
7210
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 solve it. My problem is: I want to import a dll in asp.net, and I have a major problem First i want to say that this dll works fine in a windows application written in C#, and It works fine when I use the ASP.NET development server (Local...
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10620
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.