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

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

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

    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.
    Code:
    from turtle import *
    from random import *
    
    s = Screen()
    s.setup(560,560)
    s.title("A turtle collecting coins!!")
    
    s.tracer(False)
    writer = Turtle(visible = False)
    writer.penup()
    writer.goto(0, -275)
    
    coins = []
    for i in range(-4,5):
      for j in range(-4, 5):
          c = Turtle(shape="circle")
          c.color("", "orange")
          c.shapesize(0.5)
          c.goto(40*i, 40*j)
          coins.append(c)
    s.tracer(True)
    
    Molly=Turtle(shape="turtle")
    Molly.color("","green")
    Molly.shapesize(1)
    Molly.goto(-175,-175)
    
    score=0
    while True:
      Molly.fd(randint(10,30))
      if Molly.distance(c)<15:
        c.color("","white")
        Molly.shapesize+=1
        score+=1
      Molly.rt(randint(0,360))
      if Molly.distance(c)<15:
        c.color("","white")
        Molly.shapesize+=1
        score+=1
      Molly.bk(randint(10,30))
      if Molly.distance(c)<15:
        c.color("","white")
        Molly.shapesize+=1
        score+=1
      Molly.bk(randint(10,30))
      if Molly.distance(c)<15:
        c.color("","white")
        Molly.shapesize+=1
        score+=1
      Molly.lt(randint(0,360))
      if Molly.distance(c)<15:
        c.color("","white")
        Molly.shapesize+=1
        score+=1
        
      if Molly.xcor()<-175 or >175:
        write("Score:"+str(score),align="center",font=("Courier",14,bold))
        break
      
      if Molly.ycor()<-175 or >175:
        write("Score:"+str(score),align="center",font=("Courier",14,bold))
        break
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

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

    Comment

    • dbenarfa
      New Member
      • Jan 2010
      • 1

      #3
      Hi

      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

      Comment

      Working...