#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Turtle program help


    how can i write a program that offers the user the following menu options:
    N- Create a new turtle
    R- Make all turtles turn right
    L- Make all turtles turn left
    M- Make all turtles move ahead
    Q- End the program

    The program keeps track of all turtles. All turtlesfollowthe command the user gives (e.g.,they all turn right, they all move ahead).
    You need to ‘store’ all your turtles in a list. Remember, a list can contain any kind of object.

    how do i even start this? please help urgently
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481

    Hey! This is fun.


    We can handle multiple turtles like this.
    Code:
    >>> import turtle
    >>> turtles = [turtle.Turtle() for i in 'ab']  # make two turtles, keep them in a list
    >>> turtles[0].forward(100)                    # move one of the turtles
    >>> [turtle.forward(100)for turtle in turtles] and None    # move all the turtles (and hide the output)
    >>> turtles.append(turtle.Turtle())            # get a new turtle
    >>> [turtle.right(90) for turtle in turtles] and None      # affect all turtles
    >>> turtles.append(turtle.Turtle())            # get a new turtle.  Fun Fun.  It has a different heading.
    >>> [turtle.forward(100)for turtle in turtles] and None
    >>>
    Or did you have questions about the menu?

    Anyway, you've got to show some work. Even an outline in pseudo python that needs translation into python would show that you've considered your homework.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    We can handle multiple turtles like this.
    Code:
    >>> import turtle
    >>> turtles = [turtle.Turtle() for i in 'ab']  # make two turtles, keep them in a list
    >>> turtles[0].forward(100)                    # move one of the turtles
    >>> [turtle.forward(100)for turtle in turtles] and None    # move all the turtles (and hide the output)
    >>> turtles.append(turtle.Turtle())            # get a new turtle
    >>> [turtle.right(90) for turtle in turtles] and None      # affect all turtles
    >>> turtles.append(turtle.Turtle())            # get a new turtle.  Fun Fun.  It has a different heading.
    >>> [turtle.forward(100)for turtle in turtles] and None
    >>>
    Or did you have questions about the menu?

    Anyway, you've got to show some work. Even an outline in pseudo python that needs translation into python would show that you've considered your homework.

    thnx, but there's an error in line 2:
    Code:
    turtles = [Turtle.turtle() for i in 'ab']  # make two turtles, keep them in a list
    Error:
    The error was:class 'Turtle' has no attribute 'turtle'
    Attribute not found.
    You are trying to access a part of the object that doesn't exist.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    I wrote

    turtle.Turtle

    you wrote

    Turtle.turtle


    They look alike but They are noT.

    turtle.Turtle
    Turtle.turtle
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    I wrote

    turtle.Turtle

    you wrote

    Turtle.turtle


    They look alike but They are noT.

    turtle.Turtle
    Turtle.turtle
    Code:
    def main():
     
    
      task = raw_input("Please enter an option:")
      if task == "n":
        pluto=makeWorld()
      
        goofy=SmartTurtle(pluto)
    
        goofy
        main()
      
      elif task == "r":
        pluto=makeWorld()
      
        goofy=SmartTurtle(pluto)
    
        
        goofy.turnRight()
        main()
        
      
      
    main()

    I did this but i it only makes one turtle, i can get it to turn right but i need it so that whenever i press on: "n" to create a new turtle, how can i save all the turtles on a list?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481

    Lost hope


    turtles = list()
    turtles.append(turtle.Turtle())


    Time and time again we see
    Code:
    def menu():
        #...
        menu()
    Recursion without exit is a terrible mistake.

    Use a looping statement instead.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo