473,386 Members | 1,721 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,386 software developers and data experts.

What's wrong with this code?

Activity 2: Find the Pyramid
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.

So here's my code.

from random import*
from turtle graphics*

class pyramid:
pyramid=Turtle()
pyramid.shape("triangle")
pyramid.color("yellow")
pyramid.shapesize(4)
pyramid.up()
pyramid.goto(randint(-300,300), randint(-300,300)
pyramid.down()

class lost_turtle:
lost_turtle=Turtle()
lost_turtle.shape("turtle")
lost_turtle.color("brown")
while (lost_turtle.distance(pyramid)<25):
lost_turtle.fd(randint(10,33))
lost_turtle.rt(randint(0,360))
My teacher said that my while loop does not work because lost_turtle and pyramid only exist in their class and I can't call it. Is there any way to fix this without putting it in one big function or putting it in two files? Help, please!
Also, tell me if you see any general errors. I just started this programming shebang, so don't be too harsh. Thanks
Dec 16 '09 #1
3 1418
bvdet
2,851 Expert Mod 2GB
Well, you are not defining your objects properly. You need to read the Python documentation or your course reference material.

Here is a simple class definition:
Expand|Select|Wrap|Line Numbers
  1. class Card(object):
  2.     # define class variables, if any
  3.     suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
  4.     rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6",
  5.                 "7", "8", "9", "10", "Jack", "Queen", "King"]
  6.     scoreList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  7.     # This method is called when the class is called as a function
  8.     def __init__ (self, suit=0, rank=1):
  9.         if suit >= 0 and suit <= 3:
  10.             self.suit = suit
  11.         else:
  12.             self.suit = 0
  13.         if rank >= 1 and rank <= 13:
  14.             self.rank = rank
  15.         else:
  16.             self.rank = 1
  17.         self.score = self.scoreList[self.rank]
  18.         self.cname = self.rankList[self.rank]
  19.         self.sname = self.suitList[self.suit]
  20.     # define your methods and overloads here
  21.     def __str__(self):
  22.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  23.  
  24.     def __repr__(self):
  25.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  26.  
  27.     def __cmp__(self, other):
  28.         i = cmp(self.rank, other.rank)
  29.         if i == 0:
  30.             return cmp(self.suit, other.suit)
  31.         return i
Some interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> Card()
  2. Ace of Hearts
  3. >>> c = Card()
  4. >>> c
  5. Ace of Hearts
  6. >>> str(c)
  7. 'Ace of Hearts'
  8. >>> c.rank
  9. 1
  10. >>> c.score
  11. 1
  12. >>> d = Card(4,5)
  13. >>> d
  14. 5 of Hearts
  15. >>> c<d
  16. True
  17. >>> d == c
  18. False
  19. >>> 
Does that help any?
Dec 16 '09 #2
Yeah, I thought that I was doing it wrong. But I'm not sure I understand this. Are you saying that I need to put my classes pyramid and lost_turtles behaviors in a def __init__ function? Sorry, could you explain how I would redifine the classes in my code. Thanks.
Dec 17 '09 #3
Glenton
391 Expert 256MB
Hi ccgrl451

You're so far from the solution, that I'm afraid you need to do this the hard way. Either find a text book, or one of the many web tutorials, or a friend or teacher and prepare for several hours of hard work.

This kind of forum is much more effective when you have specific questions.

Good luck
Dec 17 '09 #4

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

Similar topics

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...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
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
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.