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

59 Views Asked by At

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'))

2

There are 2 best solutions below

1
On

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'))
1
On

You keep data in numpy.array so you can do it in one line - without index.

float_mean_times = (float_op_time + float_ltime + float_ptime) / 3

And this gives directly new numpy.array so it doesn't need np.array(...).astype(float)


And if you have it as normal lists then you would need for-loop with zip()

mean_times = []

for o, l, t in zip(float_op_time, float_ltime, float_ptime):
    average = (o + l + t) / 3
    mean_times.append(average)

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

Frankly, I see one problem - user may give different number of values for different arrays and then first version will not work, and second version will calculate it only for the shortest number of data.

I would rather use one while-loop to make sure user put the same number of data for all lists.

import numpy as np
from tabulate import *

tasks = []
op_time = []
ltime = []
ptime = []

while True:
    
    t = input("Label for single TASK: ")
    if t == '!stop':
        break
    
    o = input("Enter OPTIMISTIC time for this single task: ")
    if o == '!stop':
        break
    
    lt = input("Enter MOST LIKELY time for this single task: ")
    if lt == '!stop':
        break

    p = input("Enter PESSIMISTIC time for this single task: ")
    if pt == '!stop':
        break

    # it will add data only if user put all answers 
    # (and it will skip task when user skip at some question)

    tasks.append(t)
    op_time.append(o)
    ltime.append(lt)
    ptime.append(p)

# ---

float_op_time = np.array(op_time).astype(float)
float_ltime = np.array(ltime).astype(float)
float_ptime = np.array(ptime).astype(float)

float_mean_times = (float_optime + float_ltime + float_ptime) / 3

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'))