范围和调用比特币价格检查器

问题描述

嗨,我目前正在制作比特币价格跟踪器。我把它装好,当比特币价格达到某个点时它会向我发送警报。我仍然是初级程序员,但到目前为止它运行良好。

我无法弄清楚我应该如何确定范围然后调用我的两个函数,而该函数不会一遍又一遍地调用自己。我需要两个函数都可以访问的变量是“current_price”。什么是范围/组织然后调用这些的正确方法,以便我的内部函数可以访问我的外部函数变量?谢谢!

代码

import smtplib
import os
import requests
import json

email_user = os.environ.get('EMAIL_USER')
email_password = os.environ.get('APP_PASS')
phone_number = os.environ.get('USER_PHONE')

target_price = 20000 # set your target price in USD

def send_email():

    with smtplib.SMTP('smtp.gmail.com',587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        smtp.login(email_user,email_password)

        subject = 'Bitcoin Alert: '
        body = 'Bitcoins price is ' + str(target_price) + "!"

        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail(email_user,phone_number,msg)

def check_price():

    response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
    data = response.json()

    currency = data["data"]["base"]
    current_price = data["data"]["amount"]
    current_price = int(float(current_price)) ## converts string float to int

    if current_price > target_price:
        send_email()

    print(f'Bitcoins current price is {current_price}')
    

check_price()



解决方法

有两种方法可以做到这一点。全局变量,通常不赞成并传入 current_price 作为参数。

全局变量 - 不赞成:

import smtplib
import os
import requests
import json

email_user = os.environ.get('EMAIL_USER')
email_password = os.environ.get('APP_PASS')
phone_number = os.environ.get('USER_PHONE')

target_price = 20000 # set your target price in USD

def send_email():

    with smtplib.SMTP('smtp.gmail.com',587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        smtp.login(email_user,email_password)

        subject = 'Bitcoin Alert: '
        body = 'Bitcoins price is ' + str(target_price) + "!"

        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail(email_user,phone_number,msg)

def check_price():
    global current_price #global variable declared
    response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
    data = response.json()

    currency = data["data"]["base"]
    current_price = data["data"]["amount"]
    current_price = int(float(current_price)) ## converts string float to int

    if current_price > target_price:
        send_email()

    print(f'Bitcoins current price is {current_price}')
    

check_price()

并传入当前价格:

import smtplib
import os
import requests
import json

email_user = os.environ.get('EMAIL_USER')
email_password = os.environ.get('APP_PASS')
phone_number = os.environ.get('USER_PHONE')

target_price = 20000 # set your target price in USD

current_price = 0 # declare current price

def send_email(current_price):

    with smtplib.SMTP('smtp.gmail.com',msg)

def check_price(current_price):

    response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
    data = response.json()

    currency = data["data"]["base"]
    current_price = data["data"]["amount"]
    current_price = int(float(current_price)) ## converts string float to int

    if current_price > target_price:
        send_email(current_price)

    print(f'Bitcoins current price is {current_price}')
    

check_price(current_price) #current price is passed in 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...