InstantNut
InstantNut

Reputation: 93

How do I print out an output from a specific index based upon 3 Lists (same position on list for all 3)

I am trying to create a program where there are 3 Lists that will print out the name, wage, and total wage based upon their index location on the list.

The length of each variable cannot just be 6, so it should accommodate any input given (name/wage/hour list could as long as desired). Ignore data validation/formatting as we assume that the user will input the info correctly all the time for all the variables.

For example, I want my desired output to be (see index 0 in the list of the 3 variables in the code):

Sanchez worked 42.0 hours at $10.00 per hour, and earned $420.00

$420.00 -> (10.00*42.0)

Currently here is my code:

name = ['Sanchez', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes']
wage = ['10.0', '18', '14.80', '15', '18', '15']
hours = [42.0, 41.5, 38.0, 21.5, 21.5, 22.5]


i = 0
for y in name:

    payOut = float(wage[i]) * float(hours[i])
    product = (name[i], wage[i], payOut)
    i += 1
    print(product)


empName = input("gimmie name: ") #no data validation neeeded

def target(i,empName):
    for x in i:
    if x == str(empName):
      empName = x #should return index of the empName
'''^^^ this is currently incorrect too as i believe it should return
the index location of the name, which I will use to print out the
final statement based upon the location of each respective variable 
list. (not sure if this works)'''



target(name,empName)

Upvotes: 1

Views: 63

Answers (3)

Cohan
Cohan

Reputation: 4564

You can simply use the list.index() method for the list. No need to iterate through everything.

emp_name = input("gimmie name: ") # Weiss

idx = name.index(emp_name) # 99% of the work is done right here.

print('{n} worked {h} hours at ${w:.2f} per hour, and earned ${p:.2f}'.format(
    n = name[idx], 
    h = hours[idx],
    w = float(wage[idx]), # Convert to float so you can show 2 decimals for currency
    p = float(wage[idx]) * hours[idx] # Calculate pay here
))

#Weiss worked 38.0 hours at $14.80 per hour, and earned $562.40

Upvotes: 2

CristiFati
CristiFati

Reputation: 41137

You could use [Python 3]: enumerate(iterable, start=0):

names = ['Sanchez', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes']
wages = ['10.0', '18', '14.80', '15', '18', '15']
hours = [42.0, 41.5, 38.0, 21.5, 21.5, 22.5]


def name_index(name_list, search_name):
    for index, item in enumerate(name_list):
        if item == search_name:
            return index
    return -1


emp_name = input("gimmie name: ")

idx = name_index(names, emp_name)
if idx == -1:
    print("Name {:s} not found".format(emp_name))
else:
    wage = float(wages[idx])
    hour = hours[idx]        
    print("{:s} worked {:.2f} hours at $ {:.2f} per hour earning $ {:.2f} ".format(names[idx], hour, wage, wage * hour))

Upvotes: 1

Angelo Mendes
Angelo Mendes

Reputation: 978

If your vectors have the same lenght, you case use range.

for i in range(0,len(name),1):
    payOut = float(wage[i]) * float(hours[i])
    product = (name[i], wage[i], payOut)
    print(product)

Upvotes: 1

Related Questions