如何在 python 中定义 FOR 循环

How to define FOR loop in python

我需要帮助。在下面的代码中,我希望 FOR 循环遍历所有列出了相关 API KEY 和 SECRET 的帐户,并将比特币从它们一个接一个地发送到收件人电子邮件地址,只要它们的余额大于零:

    #!/data/data/com.termux/files/usr/bin/python3.8

from coinbase.wallet.client import Client
import json

api_key1 = '<key>'
api_secret1 = '<secret>'
api_key2 = '<key>'
api_secret2 = '<secret>'
api_key3 = '<key>'
api_secret3 = '<secret>'
api_key4 = '<key>'
api_secret4 = '<secret>'
api_key5 = '<key>'
api_secret5 = '<secret>'
client = Client(api_key, api_secret)
accounts = client.get_accounts()['data']
for account in accounts:
    sending_currency = account['currency']
    if float(account['balance']['amount']) > 0:
        #Send to other account
        sending_account = client.get_account(account['id'])
        sending_amount = account['balance']['amount']
        print('sending %s %s from SENDER_EMAIL_ADDRESS' %(sending_amount, sending_currency))
        sending_account.send_money(to = 'RECEPIENT_EMAIL_ADDRESS', amount = sending_amount, currency = sending_currency)

为了实现你所描述的,我会使用一个字典列表,这也是可迭代的,因此可以在 for 循环中使用。为了做到这一点,我会这样重写你的代码:

#!/data/data/com.termux/files/usr/bin/python3.8

from coinbase.wallet.client import Client
import json
credentials = [
{'api_key':'<key1>', 'api_secret':'<secret1>'},
{'api_key':'<key2>', 'api_secret':'<secret2>'},
{'api_key':'<key3>', 'api_secret':'<secret3>'}
........
]
while True:
    for credential in credentials:
        client = Client(credential['api_key'], credential['api_secret'])
        accounts = client.get_accounts()['data']
        for account in accounts:
            sending_currency = account['currency']
            if float(account['balance']['amount']) > 0:
                 #Send to other account
                 sending_account = client.get_account(account['id'])
                 sending_amount = account['balance']['amount']
                 print('sending %s %s from SENDER_EMAIL_ADDRESS' %(sending_amount, sending_currency))
                 sending_account.send_money(to = 'RECEPIENT_EMAIL_ADDRESS', amount = sending_amount, currency = sending_currency)
            else:
                ........