简体   繁体   中英

Appending Two-Dimensional Lists in Python

first post on here so take it easy on me.

I have been stuck on this program for my python class for a little while now and am not sure what I need to do to proceed.

I am getting errors on when I run my create_commission() function. I am pretty sure it is because I should not be using count[] but am not sure what I should be using. I am going off of my professors pseudocode which shows the following:

create_commission(s_list)
create c_list
for every person list in s_list
    create s_comm list
    s_comm[0] = person[0]
    s_comm[1] = person[1]
    s_comm[2] = find_commission(person[2])
    append s_comm to c_list
return c_list

I am not sure what I should be putting in the person[] spot.

Any and all help would be appreciated!

SENTINEL = 'XX'

def main():
    sales_force = []
    commission = []
    sales_force = create_sales_force()
    commission = create_commission(sales_force)


def create_sales_force():
    s_force = []
    last_name = input("Employee's Last Name (xx to quit): ")
    while last_name.upper() != SENTINEL:
        first_name = input("Employee's First Name: ")
        valid_sales_amount = False
        while not valid_sales_amount:
            try:
                sales_amount = float(input("Sales Amount: "))
            except ValueError:
                print("-- INVALID SALES AMOUNT --")
            else:
                if 0 <= sales_amount <= 50000:
                    valid_sales_amount = True
                else:
                    print("**INVALID SALES AMOUNT**")
                    print("Number must be between 0 and 50,000")

        sales_person_list = [last_name, first_name, sales_amount]
        s_force.append(sales_person_list)

        last_name = input("Employee's Last Name (xx to quit): ")

    return s_force

def create_commission(s_list):
    c_list = []
    for count in range(0, len(s_list)):
        s_comm = []
        s_comm[0] = count[0]
        s_comm[1] = count[1]
        s_comm[2] = find_commission(count[2])
        c_list.append(s_comm)

    return c_list



def find_commission(sales_amt):
    if sales_amt <= 5000:
        return sales_amt * .08
    elif 5000 < sales_amt <= 10000:
        return sales_amt * .11
    elif 10000 < sales_amt <= 18000:
        return sales_amt * .14
    elif 18000 < sales_amt <= 50000:
        return sales_amt * .18


main()

The variable count is an int , and you can't index an int . What I think you're trying to do is to create, for each employee, an entry to a the list c_list . What you can do, is to use count as an index for the list s_list , such as:

def create_commission(s_list):
    c_list = []
    for count in range(0, len(s_list)):
        s_comm = []
        s_comm.append(s_list[count][0])
        s_comm.append(s_list[count][1])
        s_comm.append(find_commission(s_list[count][2]))
        c_list.append(s_comm)

    return c_list

Note that s_comm is initially empty, so you have to append new component to that list, and you cannot directly index them (they do not exist before append ing new data)

Moreover, there is no need to use the the functions range and len , you can iterate directly on the element of the list with

def create_commission(s_list):
    c_list = []
    for e in s_list:
        s_comm = []
        s_comm.append(e[0])
        s_comm.append(e[1])
        s_comm.append(e[2])
        c_list.append(s_comm)

return c_list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM