简体   繁体   中英

2-D List and Indexing

Write a function passenger_baggage() that has two parameters: p and b, where p is the passenger number and b is the bag number. In your function, assign the following 2-D list to a variable as such:

m = [[45, 35, 52, 75], [25, 30, 65 ], [50, 43, 32, 22 ]]

The matrix or 2-D list m shows bag weights in pounds for three passengers. The first passenger has 4 bags, the second passenger has 3 bags and the third passenger has 4 bags.

Your function will use a row and column index as discussed during the lecture to display passenger information as shown in Example, using format() string method

I have this:

def passenger_baggage(p,b):
    'Where p is the passenger number and b is the number'
    m = [[ 45,35,52,75],[25,30,65], [50,43,32,22,]]

    for i in range (p[]b[])
    print ('bag #{} , for passenger number #{} weighs lb'.format(b,p))
def passenger_baggage(p, b):
    '''Print the weight of the bag b for passenger p.'''

    m = [[ 45,35,52,75],[25,30,65], [50,43,32,22,]]

    try:
        print('bag #{}, for passenger #{} weighs {} lb'.format(b, p, m[p][b])
    except IndexError:
        print('Passenger #{} either doesn't exist or has no bag #{}.format(p,b))

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