范围和呼叫比特币价格检查器



嗨,我目前正在制作比特币价格跟踪器。我把它安装好了,这样当比特币的价格达到某个点时,它就会给我发警报。我还是个初级程序员,但到目前为止它运行得很好。

我有麻烦弄清楚我应该如何作用域,然后调用我的两个函数没有函数调用自己一遍又一遍。我需要两个函数访问的变量是'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}nn{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}nn{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', 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}nn{body}'
smtp.sendmail(email_user, phone_number, 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 

最新更新