来自包含在 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。