【问题标题】:How to define FOR loop in python如何在python中定义FOR循环
【发布时间】:2020-05-19 04:34:23
【问题描述】:

我在这里需要帮助。在下面的代码中,我希望 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)

【问题讨论】:

  • 您面临的问题是什么?什么不起作用?
  • 它适用于单个帐户。但是我想修改脚本来循环遍历指定了 API 和 SECRET 的 5 个帐户。

标签: python-3.x for-loop coinbase-api termux


【解决方案1】:

为了达到你所描述的,我会使用一个字典列表,这也是可迭代的,因此可以在 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:
                ........

【讨论】:

  • P.S.我对这个项目很感兴趣。删除链接,我会帮助你。我可以看到这是怎么回事。 XD
  • 我运行了你的代码,这就是我得到的python Python 3.8.0 (default, Dec 5 2019, 10:53:43) [Clang 8.0.7 (https://android.googlesource.com/toolchain/clang b55f2d4ebfd35bf6 on linux Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import test2 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/test2.py", line 6 {api_key = '&lt;key1&gt;', api_secret = '&lt;secret1&gt;'}, ^ SyntaxError: invalid syntax &gt;&gt;&gt;
  • 该代码的第 6 行可能在 credentials = [...] 行看到了语法错误
  • 是的,我打错了一个报价,现在尝试运行程序
  • 说的是同一件事
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
相关资源
最近更新 更多