473,387 Members | 1,290 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.

What's wrong with this code, part 2.

Okay, so you probably haven't seen my previous question, whats wrong with this code. For those who have, you know what I'm talking about. If not, heres what I have to do.

Randomly place a pyramid (yellow, large turtle) on the screen. Have a second turtle move
randomly until it is close to the pyramid. Stop the seeking turtle once it has found the pyramid.
We have to do this using object-oriented coding. We don't have to use classes, but I just chose to do so. This also needs to happen in turtle graphics. Heres my code:

Expand|Select|Wrap|Line Numbers
  1. from turtle import *
  2. from random import *
  3.  
  4. def find_the_pyramid():
  5.     class Pyramid(Turtle()):
  6.         color="yellow"
  7.         shape="triangle"
  8.         shapesize=4
  9.         def __init__(self,color,shape,shapesize):
  10.             self.color=color
  11.             self.shape=shape
  12.             self.shapesize=shapesize
  13.             self.goto(randint(-100,100),randint(-100,100))
  14.             self.goto(randint(-100,100),randint(-100,100))
  15.  
  16.  
  17.     class Lost_Turtle(Turtle()):
  18.         name="Kirk"
  19.         color="brown"
  20.         shape="turtle"
  21.         shapesize=2
  22.         def __init__(self,name,color,shape,shapesize):
  23.             self.name=name
  24.             self.color=color
  25.             self.shape=shape
  26.             self.shapesize=shapesize
  27.  
  28.     Sphinx=Pyramid("yellow","triangle",5)
  29.     Billy=Lost_Turtle("Billy","blue","turtle",3)
  30.  
  31.  
  32.     while(Billy.distance(Sphinx)>20):
  33.         Billy.forward(randint(40,80))
  34.         Billy.right(randint(0, 360))
  35.  
  36. find_the_pyramid()
The only problem is that I keep getting sent errors, which I don't understand. If so, can you take a look at my code and try to tell me what I'm doing wrong. Much appreaciated. Also, if there's an easier way to do this without using classes, that would be great. Thanks.
Dec 19 '09 #1
2 1949
bvdet
2,851 Expert Mod 2GB
ccgrl451,

Please use code tags when posting code. See posting guidelines here. I appreciate your creative use of indentation, but code tags are much better suited to displaying code.

To inherit from another class, leave off the parentheses:
Expand|Select|Wrap|Line Numbers
  1. class Pyramid(Turtle):
There is no need to create class variables in your case. You can assign defaults like this:
Expand|Select|Wrap|Line Numbers
  1.     def __init__(self, color='yellow', shape='triangle', shapesize=4):
I don't see how you can be sure you will ever find the pyramid by moving only forward and right. You should check the turtle's position after every move. In your while loop, you are moving twice before checking the location. I would do something like this:

Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     # 'a' and 'b' can be any numbers of your choice
  3.     Billy.forward(randint(a,b))
  4.     if Billy.distance(Sphinx)>20:
  5.         break
  6.     Billy.right(randint(a,b))
  7.     if Billy.distance(Sphinx)>20:
  8.         break
  9.     Billy.backward(randint(a,b))
  10.     if Billy.distance(Sphinx)>20:
  11.         break
  12.     Billy.left(randint(a,b))
  13.     if Billy.distance(Sphinx)>20:
  14.         break
  15. # summarize the results here
  16. return 'summarized results'
I would not encapsulate the classes and movement in the same function. I would suggest this format:

imports
class 1 definition
class 2 definition
define function "main()" where you create class instances and random movements and return results
Then:
Expand|Select|Wrap|Line Numbers
  1. if __name__ == "__main__":
  2.     main()
Dec 19 '09 #2
Sorry this took too long to check. I was busy. Thanks this helped me tons. Also, the link to the posting guidelines are great for future reference!
Jan 2 '10 #3

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
7
by: Vic | last post by:
Dear All, I found this code snippet on this list (taken from a nice webpage of a courteous fellow), which I used to filter a form on a combo box. I wanted to repeat the same code to have an...
15
by: ben | last post by:
this programme from sedgewick's 'algorithms in c parts 1-4' "is a sample client program that ... uses a symbol table to find the distinct values in a sequence of keys (randomly generated or read...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
1
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this...
10
by: Enkidu | last post by:
Beginner question, sorry! I am using indexers to access an array of StringBuilders in an instance of a class: Getting: value = board1; Setting: board1 = value1 ;
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.