簡體   English   中英

IndexError:元組索引超出范圍--- Python

[英]IndexError: tuple index out of range --— Python

我正在運行一個簡單的代碼,它將讀取json文件,獲取輸入並生成輸出。 但是在其中之一,我得到了錯誤

activities_obj = document.add_paragraph()
activities_obj = helper.format_alignment(activities_obj)
activities = helper.askForChoices(activities, "Choose extracurricular activities")
activity_string = "Beside coop experience, {}. Additionally, {}. {}. Last but not least, {}."
activiy_para = activity_string.format(*activities)

這是代碼,最后一行給出了錯誤:

activiy_para = activity_string.format(*activities)
IndexError: tuple index out of range

謝謝

您沒有將足夠的參數傳遞給activity_string.format(*activities)

您需要通過4個鍵傳遞dict,

Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> activity_string = "Beside coop experience, {}. Additionally, {}. {}. Last but not least, {}."
>>> activities = {'a': '1', 'b': '2', 'c': '3', 'd': '4'}
>>> activity_string.format(*activities)
'Beside coop experience, a. Additionally, b. c. Last but not least, d.'
>>>

如果您沒有傳遞足夠多的參數,

Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> activity_string = "Beside coop experience, {}. Additionally, {}. {}. Last but not least, {}."
>>> activities = {'a': '1'}
>>> activity_string.format(*activities)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM