IndexError: list index out of range with api
IndexError: list index out of range with api
all_currencies = currency_api('latest', 'currencies') # {'eur': 'Euro', 'usd': 'United States dollar', ...}
all_currencies.pop('brl')
qtd_moedas = len(all_currencies)
texto = f'{qtd_moedas} Moedas encontradas\n\n'
moedas_importantes = ['usd', 'eur', 'gbp', 'chf', 'jpy', 'rub', 'aud', 'cad', 'ars']
while len(moedas_importantes) != 0:
for codigo, moeda in all_currencies.items():
if codigo == moedas_importantes[0]:
cotacao, data = currency_api('latest', f'currencies/{codigo}/brl')['brl'], currency_api('latest', f'currencies/{codigo}/brl')['date']
texto += f'{moeda} ({codigo.upper()}) = R$ {cotacao} [{data}]\n'
moedas_importantes.remove(codigo)
if len(moedas_importantes) == 0: break # WITHOUT THIS LINE, GIVES ERROR
为什么会出现此错误?该列表实际上用完了元素,但代码仅适用于 if
你有一个嵌套循环。进入 while 循环,然后立即在 for 循环中开始执行。对于 all_currencies.items()
中的所有元素,执行保留在 for 循环中。每次在 moedas_importantes
的开头找到 codigo
时,都会删除该元素。最终您从 moedas_importantes
中删除了所有元素,但您仍在 for 循环中并检查 if codigo == moedas_importantes[0]
。此时 moedas_importantes
是空的所以你得到一个索引错误。
我认为下面的代码可以在没有嵌套循环的情况下运行。请注意,这假设 moedas_importantes
中的所有元素也是 all_currencies
.
中的键
all_currencies = currency_api('latest', 'currencies') # {'eur': 'Euro', 'usd': 'United States dollar', ...}
all_currencies.pop('brl')
qtd_moedas = len(all_currencies)
texto = f'{qtd_moedas} Moedas encontradas\n\n'
moedas_importantes = ['usd', 'eur', 'gbp', 'chf', 'jpy', 'rub', 'aud', 'cad', 'ars']
while len(moedas_importantes) != 0:
codigo = moedas_importantes[0]
moeda = all_currencies[codigo]
cotacao, data = currency_api('latest', f'currencies/{codigo}/brl')['brl'], currency_api('latest', f'currencies/{codigo}/brl')['date']
texto += f'{moeda} ({codigo.upper()}) = R$ {cotacao} [{data}]\n'
moedas_importantes.remove(codigo)
all_currencies = currency_api('latest', 'currencies') # {'eur': 'Euro', 'usd': 'United States dollar', ...}
all_currencies.pop('brl')
qtd_moedas = len(all_currencies)
texto = f'{qtd_moedas} Moedas encontradas\n\n'
moedas_importantes = ['usd', 'eur', 'gbp', 'chf', 'jpy', 'rub', 'aud', 'cad', 'ars']
while len(moedas_importantes) != 0:
for codigo, moeda in all_currencies.items():
if codigo == moedas_importantes[0]:
cotacao, data = currency_api('latest', f'currencies/{codigo}/brl')['brl'], currency_api('latest', f'currencies/{codigo}/brl')['date']
texto += f'{moeda} ({codigo.upper()}) = R$ {cotacao} [{data}]\n'
moedas_importantes.remove(codigo)
if len(moedas_importantes) == 0: break # WITHOUT THIS LINE, GIVES ERROR
为什么会出现此错误?该列表实际上用完了元素,但代码仅适用于 if
你有一个嵌套循环。进入 while 循环,然后立即在 for 循环中开始执行。对于 all_currencies.items()
中的所有元素,执行保留在 for 循环中。每次在 moedas_importantes
的开头找到 codigo
时,都会删除该元素。最终您从 moedas_importantes
中删除了所有元素,但您仍在 for 循环中并检查 if codigo == moedas_importantes[0]
。此时 moedas_importantes
是空的所以你得到一个索引错误。
我认为下面的代码可以在没有嵌套循环的情况下运行。请注意,这假设 moedas_importantes
中的所有元素也是 all_currencies
.
all_currencies = currency_api('latest', 'currencies') # {'eur': 'Euro', 'usd': 'United States dollar', ...}
all_currencies.pop('brl')
qtd_moedas = len(all_currencies)
texto = f'{qtd_moedas} Moedas encontradas\n\n'
moedas_importantes = ['usd', 'eur', 'gbp', 'chf', 'jpy', 'rub', 'aud', 'cad', 'ars']
while len(moedas_importantes) != 0:
codigo = moedas_importantes[0]
moeda = all_currencies[codigo]
cotacao, data = currency_api('latest', f'currencies/{codigo}/brl')['brl'], currency_api('latest', f'currencies/{codigo}/brl')['date']
texto += f'{moeda} ({codigo.upper()}) = R$ {cotacao} [{data}]\n'
moedas_importantes.remove(codigo)