Home > OS >  Python code for adding 3 corresponding lists elements and getting averages from user input
Python code for adding 3 corresponding lists elements and getting averages from user input

Time:05-12

For my code I need help with "mean_times[]" I want to take the corresponding list items from all first elements from "op_time", "ltime", and "ptime" add them up, then divide by three and then all second elements to add them up and divide by three, and so on as many times as the user inputs tasks.

The way I wrote my code is static. I allow for three instances of this. but the code allows the user to input as many tasks as they want. I think I need the code to append to "mean_times[]" as many times as there are user inputs. Meaning to take "(float_optime[0] float_ltime[0] float_ptime[0]) / 3" and append that into mean_times[] then do the same for the second element, and so on as many times as there are tasks.

I'm not sure of the logic I need or what sort of loop I need to do to add then append the product into mean_times[]

import numpy as np
from tabulate import *

tasks = []
t = input("Label the TASKS (a, b, c,..., entering STOP would end the process): ")
tasks.append(t)
while t.lower() != 'stop':
    t = input("Label the TASKS (a, b, c, ..., entering STOP would end the process): ")
    tasks.append(t)
del tasks[-1]

op_time = []
o = input("Enter OPTIMISTIC time for each task: ")
op_time.append(o)
while o.lower() != 'stop':
    o = input("Enter OPTIMISTIC time for each task: :")
    op_time.append(o)
del op_time[-1]

ltime = []
lt = input("Enter MOST LIKELY time for each task: ")
ltime.append(lt)
while lt.lower() != 'stop':
    lt = input("Enter MOST LIKELY time for each task: :")
    ltime.append(lt)
del ltime[-1]

ptime = []
p = input("Enter PESSIMISTIC time for each task: ")
ptime.append(p)
while p.lower() != 'stop':
    p = input("Enter PESSIMISTIC time for each task: :")
    ptime.append(p)
del ptime[-1]

array_op_time = np.array(op_time)
float_op_time = array_op_time.astype(float)

array_ltime = np.array(ltime)
float_ltime = array_ltime.astype(float)

array_ptime = np.array(ptime)
float_ptime = array_ptime.astype(float)

mean_times = [(float_optime[0]   float_ltime[0]   float_ptime[0]) / 3, (float_optime[1]   float_ltime[1]   float_ptime[1]) / 3, (float_optime[2]   float_ltime[2]   float_ptime[2]) / 3]

array_mean_times = np.array(mean_times)
float_mean_times = array_mean_times.astype(float)

#some logic to append to mean_times

table = {"Task": tasks, "Optimistic": op_time, "Most Likely": ltime, "Pessimistic": ptime, "Mean Duration": mean_times}
print(tabulate(table, headers='keys', tablefmt='fancy_grid', stralign='center', numalign='center'))

CodePudding user response:

I'm getting close but the formatting in the table isn't what it needs to be.

I added:

mean_times = []
mean = [(a   b   c) / 3 for a, b, c in zip(float_op_time, float_ltime, float_ptime)]
mean_times.append(mean)

array_mean_times = np.array(mean_times)
float_mean_times = array_mean_times.astype(float)

however the output to the table isn't properly displaying

CodePudding user response:

Fixed the program

import numpy as np
from tabulate import tabulate

tasks = []
counter = 1
t = input("Label the TASKS (a, b, c,..., entering STOP would end the process): ")
tasks.append(t)
while t.lower() != 'stop':
    t = input("Label the TASKS (a, b, c, ..., entering STOP would end the process): ")
    tasks.append(t)
    counter = counter   1
del tasks[-1]

op_time = []
for x in range(counter-1):
    o = input("Enter OPTIMISTIC time for each task: ")
    op_time.append(o)

ltime = []
for x in range(counter-1):
    lt = input("Enter MOST LIKELY time for each task: ")
    ltime.append(lt)

ptime = []
for x in range(counter-1):
    p = input("Enter PESSIMISTIC time for each task: ")
    ptime.append(p)

array_op_time = np.array(op_time)
float_op_time = array_op_time.astype(float)

array_ltime = np.array(ltime)
float_ltime = array_ltime.astype(float)

array_ptime = np.array(ptime)
float_ptime = array_ptime.astype(float)

mean_times = []
for x in range(0, counter-1):
    m_time = ((float(ltime[x])   float(ptime[x]) float(op_time[x]))/3)
    mean_times.append(m_time)
    array_mean_times = np.array(mean_times)
    float_mean_times = array_mean_times.astype(float)

table = {"Task": tasks, "Optimistic": op_time, "Most Likely": ltime, "Pessimistic": ptime, "Mean Duration": mean_times}
print(tabulate(table, headers='keys', tablefmt='fancy_grid', stralign='center', numalign='center'))
  • Related