What's wrong with this code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccgrl451
    New Member
    • Nov 2009
    • 8

    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.shapesi ze(4)
    pyramid.up()
    pyramid.goto(ra ndint(-300,300), randint(-300,300)
    pyramid.down()


    class lost_turtle:
    lost_turtle=Tur tle()
    lost_turtle.sha pe("turtle")
    lost_turtle.col or("brown")

    while (lost_turtle.di stance(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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    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:
    Code:
    >>> 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?

    Comment

    • ccgrl451
      New Member
      • Nov 2009
      • 8

      #3
      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.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        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

        Comment

        Working...