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
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: - class Card(object):
-
# define class variables, if any
-
suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
-
rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6",
-
"7", "8", "9", "10", "Jack", "Queen", "King"]
-
scoreList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
-
# This method is called when the class is called as a function
-
def __init__ (self, suit=0, rank=1):
-
if suit >= 0 and suit <= 3:
-
self.suit = suit
-
else:
-
self.suit = 0
-
if rank >= 1 and rank <= 13:
-
self.rank = rank
-
else:
-
self.rank = 1
-
self.score = self.scoreList[self.rank]
-
self.cname = self.rankList[self.rank]
-
self.sname = self.suitList[self.suit]
-
# define your methods and overloads here
-
def __str__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __repr__(self):
-
return self.rankList[self.rank] + " of " + self.suitList[self.suit]
-
-
def __cmp__(self, other):
-
i = cmp(self.rank, other.rank)
-
if i == 0:
-
return cmp(self.suit, other.suit)
-
return i
Some interaction: - >>> Card()
-
Ace of Hearts
-
>>> c = Card()
-
>>> c
-
Ace of Hearts
-
>>> str(c)
-
'Ace of Hearts'
-
>>> c.rank
-
1
-
>>> c.score
-
1
-
>>> d = Card(4,5)
-
>>> d
-
5 of Hearts
-
>>> c<d
-
True
-
>>> d == c
-
False
-
>>>
Does that help any?
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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 ...
|
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) {
<...>
}
|
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=
|
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;
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |