How do I go about a program involving looping through a menu of calculation items?

I'm trying to write a program for performing various calculations functions in the menu below:

  1. Cartesian distance
  2. Vector x matrix
  3. Normal Vector
  4. Quit

Each time the program is run, the above menu is to be presented with an input command requesting user to input value for item on the menu. For inputting 1, 2 or 3, input data are requested and executed for that program accordingly and the menu is presented over and over again until user inputs 4 (for Quit) to stop the program.

Here's what is expected for item #2 3-by-3 matrix multiplied by a 3-value vector

I'm new to programming and we've treated data types, conditionals, loops and functions in our programming class but I'm struggling to put them all together for this program. I wrote individual programs for items 1 and 3 and they worked but my issue is putting them all together to perform the function described above.

Here are my codes:

import math     # import in-built math module for use in program

def main_menu():     # create function called "menu" for the calculator menu
    print("Welcome to the Calculator Project\n")
    print("Please choose from the menu below\n")
    print("1. Cartesian distance")
    print("2. Vector x matrix")
    print("3. Normalize")
    print("4. Quit")


menu_input = int(input("Enter command: "))

menu = "1234"

for item in menu:
    if menu_input == 1: 
        def cartesian_distance(x1, y1, x2, y2): # create a function for calculating cartesian distance with four inputs
            result_cartesian = math.sqrt((math.pow((x2 - x1), 2)) + (math.pow((y2 - y1), 2)))
            return result_cartesian

            # get user input as a string    
            user_input = input("Enter four values for x1, y1, x2 and y2, separated by a space: ").split()

            # convert user input into a floating list
            for i in range(len(user_input)):
                user_input[i] = float(user_input[i])

            # convert each member of the user_input floating list into a variable on its own
            x_1 = user_input[0]
            y_1 = user_input[1]
            x_2 = user_input[2]
            y_2 = user_input[3]

            # call function cartesian_distance with discretized user input and assign to cart_dist
            cart_dist = cartesian_distance(x_1, y_1, x_2, y_2)
            print("The cartesian distance between points ({}, {}) and ({} and {}) is {:.2f}".format(x_1, y_1, x_2, y_2, cart_dist)) 


    elif menu_input == 2: 
        print("Supposed to use loops to multiply a 3-by-3 matrix with a 3-value vector but I have no idea how to go about it!")

    elif menu_input == 3:
        # creates a function called normalize for calculating normal vector
        def normalize(v1, v2, v3):
            length = math.sqrt(math.pow(v1, 2) + math.pow(v2, 2) + math.pow(v3, 2))
            vec_1 = v1 / length
            vec_2 = v2 / length
            vec_3 = v3 / length
            normal_vector = print("\nThe normal vector for {}, {} and {} is [{:.2f}, {:.2f}, {:.2f}]".format(v_1, v_2, v_3, vec_1, vec_2, vec_3))
            #list.normal_vector = [v1 / length, v2 / length, v3 / length]
            return normal_vector

            # get vector input as a string
            vector_input = input("Enter three values for v1, v2 and v3, separated by a space: ").split()

            # convert vector input into a floating list
            for i in range(len(vector_input)):
                vector_input[i] = float(vector_input[i])

            # convert each member of the vector input floating list into a variable on its own
            v_1 = vector_input[0]
            v_2 = vector_input[1]
            v_3 = vector_input[2]

            # call function "normalize" with discretized vector input and assign to NV
            NV = normalize(v_1, v_2, v_3)

    elif menu_input == 4: 
        print("Invalid input") 

Read more here:

Content Attribution

This content was originally published by Bans at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: