My code is fully functional, but I know that isn't the main point of programming. A few of my functions work in ways I know they probably shouldn't but I really just don't know how to fix them. Any other general pointers are appreciated as well, trying to learn as much as I can.
Here's the code:

from visual import *
import random
scene.autoscale = 0

#initializations
axes = [(1,0,0), (0,1,0), (0,0,1)]
shapeList = []
colorList = [color.red, color.blue, color.green, color.white, color.orange, color.magenta]


def get_location():
    """Gets the location of the point where the user clicks."""
    print "\nClick where you would like to place the shape.\n"
    while True:
        if scene.mouse.clicked:
            m = scene.mouse.getclick()
            loc = m.pos
            return loc   

def display_menu():
    """Displays a menu allowing the user to choose what to do."""
    print \
    """
            Shapes Menu
            
        1 - Create a shape
        2 - Recolor a shape
        3 - Recolor all shapes
        4 - Delete a shape
        5 - Delete all shapes
        6 - Party
        7 - Exit
        
        """

    choice = raw_input("Choice: ")

    return choice
            
def create_shape():
    """Creates a shape of the user's choice."""
    shape_to_create = raw_input("Shape to create (cube, sphere, cylinder): ")

    if shape_to_create == "cube":
        loc = get_location()
        shape = box(pos = loc, color = random.choice(colorList), size = (1,1,1), axis = random.choice(axes))
        shapeList.append(shape)
    elif shape_to_create == "sphere":
        loc = get_location()
        shape = sphere(pos = loc, color = random.choice(colorList), radius = 1, axis = random.choice(axes))
        shapeList.append(shape)
    elif shape_to_create == "cylinder":
        loc = get_location()
        shape = cylinder(pos = loc, color = random.choice(colorList), radius = 1, axis = random.choice(axes))
        shape.length = 3
        shapeList.append(shape)
    else:
        print "\nSorry, that is not a valid choice."
    
    
def recolor_shape():
    """Returns the shape the user clicks so it may be recolored."""
    if shapeList:
        print "Click on the shape you would like to recolor."
    while shapeList:
        if scene.mouse.clicked:
            m = scene.mouse.getclick()
            if m.pick:
                return m.pick

def recolor_shapes():
    """Recolors all shapes to whatever the user says."""
    global shapeList
    color_to_make = raw_input("What color would you like to make the shapes? \
(red, blue, green, white, orange, magenta, or randomize): ")
    for shape in shapeList:
        if color_to_make == "red":
            shape.color = color.red
        elif color_to_make == "blue":
            shape.color = color.blue
        elif color_to_make == "green":
            shape.color = color.green
        elif color_to_make == "white":
            shape.color = color.white
        elif color_to_make == "orange":
            shape.color = color.orange
        elif color_to_make == "magenta":
            shape.color = color.magenta
        elif color_to_make == "randomize":
            shape.color = random.choice(colorList)
    
def delete_shape():
    """Returns a shape so that it may be deleted."""
    print """Click on the shape you would like to delete.
Be warned, that exact shape will be gone forever."""
    while shapeList:
        if scene.mouse.clicked:
            m = scene.mouse.getclick()
            if m.pick:
                return m.pick

def delete_shapes():
    """Deletes all shapes after getting confirmation."""
    global shapeList
    response = raw_input("\nAre you sure you want to delete all shapes? (y/n): ")
    if response == "y":
        for shape in shapeList:
            shape.visible = 0
        shapeList = []
        print "\nCompleted.\n"
    if response == "n":
        print "\nAborted.\n"
    
def party_mode():
    """Flashes colors of all current shapes."""
    global shapeList
    for i in range(500):
        rate(50)
        for shape in shapeList:
            shape.color = random.choice(colorList)
            
def main():
    """The main function which calls the other functions."""
    
    choice = None

    while choice != "7":

        choice = display_menu()
        
        if choice == "1":
            create_shape()
            
        elif choice == "2":
            shape = recolor_shape()
            if shape:

                color_to_make = raw_input("What color would you like to make the shape?\
 (red, blue, green, white, orange, magenta): ")

                if color_to_make == "red":
                    shape.color = color.red
                elif color_to_make == "blue":
                    shape.color = color.blue
                elif color_to_make == "green":
                    shape.color = color.green
                elif color_to_make == "white":
                    shape.color = color.white
                elif color_to_make == "orange":
                    shape.color = color.orange
                elif color_to_make == "magenta":
                    shape.color = color.magenta
            else:
                print "There are no shapes to recolor."
                
        elif choice == "3":
            recolor_shapes()
            
        elif choice == "4":
            shape_to_delete = delete_shape()
            if shape_to_delete:
                shape_to_delete.visible = 0
                shapeList.remove(shape_to_delete)
            else:
                print "\nThere are no shapes to delete.\n"

        elif choice == "5":
            delete_shapes()
            
        elif choice == "6":
            party_mode()

        elif choice == "7":
            print "Good-bye."
            
        else:
            print "\nSorry, that is not a valid choice."

main()

Recommended Answers

All 2 Replies

You need to specify the functions names and the problem that you are facing.

You need to specify the functions names and the problem that you are facing.

Like I said there aren't really any problems, I'm just looking for some critique on how I wrote and organized it. For example, see how recolor_shape just gets a shape and then main does the actual recoloring, while recolor_shapes does it itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.