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

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

如何通過(guò)用戶輸入訪問(wèn)特定字典?

如何通過(guò)用戶輸入訪問(wèn)特定字典?

開(kāi)心每一天1111 2021-09-14 13:48:27
我定義了兩個(gè)字典dict1和dict2. 我希望用戶通過(guò)輸入告訴我要訪問(wèn)哪個(gè)字典(當(dāng)然他必須知道確切的名稱),所以他從這個(gè)字典中獲取一個(gè)值。以下不起作用,我得到一個(gè)類型錯(cuò)誤“字符串索引必須是整數(shù)”:dict1 = {'size': 38.24, 'rate': 465}dict2 = {'size': 32.9, 'rate': 459}name = input('Which dictionary to access?: ')ret = name['size']print ('Size of ' + name + ' is ' + str(ret))
查看完整描述

2 回答

?
Cats萌萌

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

dict1 = {'size': 38.24, 'rate': 465}

dict2 = {'size': 32.9, 'rate': 459}


name = input('Which dictionary to access?: ')


if name == 'dict1':

  ret = dict1['size']

eif name == 'dict2':

  ret = dict2['size']



print ('Size of ' + name + ' is ' + str(ret))

或者


   input_to_dict_mapping = {'dict1':dict1,'dict2':dict2}

   ret = input_to_dict_mapping[name]['size']

或來(lái)自 Antwane 的回應(yīng)。


更新


input_to_dict_mapping = globe()

ret = input_to_dict_mapping[name]['size']

問(wèn)題是name is a string value。你不能像我們?cè)?Dict 中那樣做索引。


查看完整回答
反對(duì) 回復(fù) 2021-09-14
  • 2 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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