从包含在for循环中的另一个类中调用变量

时间:2019-02-19 02:49:51

标签: python django

for循环中包含的另一个类的变量。 调用时出现问题

看我的例子

试图四处移动代码并创建全局变量,但是没有用。

import requests

class Values:

    def get_crypto_data_dict(self):
        usd_url = "https://api.coingecko.com/api/v3/coins/markets?" \
                  "vs_currency=usd&order=market_cap_desc&per_page=250&page=1" \
                  "&sparkline=false&price_change_percentage=24h"
        gbp_url = "https://api.coingecko.com/api/v3/coins/markets?" \
                  "vs_currency=gbp&order=market_cap_desc&per_page=250&page=1" \
                  "&sparkline=false&price_change_percentage=24h"

        previous_request = None

        crypto_dict = None
        crypto_dict = dict()

        requests1 = requests.get(usd_url)
        results1 = requests1.json()

        requests2 = requests.get(gbp_url)
        results2 = requests2.json()

        for i in range(0, 250):
            crypto_dict[results1[i]['id']] = {
                'coin_name': results1[i]['name'],
                'changes': results1[i]['price_change_percentage_24h'],
                'usd': results1[i]['current_price'],
                'gbp': results2[i]['current_price']
            }


    # print(crypto_dict['bitcoin']['coin_name']) = will show coin name


class Portfolio(Values):

    def __init__(self, coin, holdings):
        self.coin = coin
        self.holdings = holdings

    def Get_User_Coins(self):
        continues = True
        coins_holdings = {}
        while (continues == False):
            coin = input("Enter the Coin Name i.e(Bitcoin(BTC) is bitcoin): ")
            holdings = input("Enter the  amount of holdings of the coin: ")
            print("\n")
            # Attatch to the current user


    def Pull_Coin_Dat(Values):
    # 'burst': {'coin_name': 'Burst', 'changes': 10.4028903177228, 'usd': 0.00425861382756581, 'gbp': 0.00329588603402332}}
    print()


    lol = Portfolio('xrp', 600)
    lol.Pull_Coin_Dat()

能够从Pull_Coin_Dat函数调用Values类中的crypto_dict。

1 个答案:

答案 0 :(得分:0)

您可以将<soapenv:Body> <oraf:queryResult xmlns:oraf="http://xmlns.oracle.com/db/SPName"/> </soapenv:Body> 的{​​{1}}字典返回为:

crypto_dict

然后以

调用get_crypto_data_dict()内部的函数
return crypto_dict

然后,您可以从Pull_Coin_Dat(self)

访问所需的任何数据

为避免缩进错误,您需要将函数调用缩进为:

called_crypto_dict = self.get_crypto_data_dict()