第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù) 3 個(gè)列表從特定索引中打印輸出

如何根據(jù) 3 個(gè)列表從特定索引中打印輸出

GCT1015 2022-01-11 19:55:30
我正在嘗試創(chuàng)建一個(gè)程序,其中有 3 個(gè)列表,這些列表將根據(jù)列表中的索引位置打印出姓名、工資和總工資。每個(gè)變量的長度不能只是 6,因此它應(yīng)該適應(yīng)任何給定的輸入(姓名/工資/小時(shí)列表可以根據(jù)需要而定)。忽略數(shù)據(jù)驗(yàn)證/格式化,因?yàn)槲覀兗僭O(shè)用戶將始終正確輸入所有變量的信息。例如,我希望我想要的輸出是(參見代碼中 3 個(gè)變量列表中的索引 0):Sanchez worked 42.0 hours at $10.00 per hour, and earned $420.00$420.00 -> (10.00*42.0)目前這里是我的代碼: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 = 0for 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 neeededdef 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 returnthe index location of the name, which I will use to print out thefinal statement based upon the location of each respective variable list. (not sure if this works)'''target(name,empName)
查看完整描述

3 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以簡單地使用list.index()列表的方法。無需遍歷所有內(nèi)容。


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


查看完整回答
反對 回復(fù) 2022-01-11
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以使用[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))



查看完整回答
反對 回復(fù) 2022-01-11
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您的向量具有相同的長度,則使用range。


for i in range(0,len(name),1):

    payOut = float(wage[i]) * float(hours[i])

    product = (name[i], wage[i], payOut)

    print(product)


查看完整回答
反對 回復(fù) 2022-01-11
  • 3 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)