界定和调用比特币价格检查器
Scoping and calling Bitcoin price checker
您好,我目前正在制作比特币价格跟踪器。我把它安装好,当比特币价格达到某个特定点时它会向我发送警报。我仍然是初级程序员,但到目前为止它运行良好。
我无法弄清楚我应该如何确定范围然后调用我的两个函数,而不是函数一遍又一遍地调用自身。我需要两个函数都可以访问的变量是 'current_price'。 scope/organize 然后调用这些的正确方法是什么,以便我的内部函数可以访问我的外部函数变量?谢谢!
代码:
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', 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(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
您好,我目前正在制作比特币价格跟踪器。我把它安装好,当比特币价格达到某个特定点时它会向我发送警报。我仍然是初级程序员,但到目前为止它运行良好。
我无法弄清楚我应该如何确定范围然后调用我的两个函数,而不是函数一遍又一遍地调用自身。我需要两个函数都可以访问的变量是 'current_price'。 scope/organize 然后调用这些的正确方法是什么,以便我的内部函数可以访问我的外部函数变量?谢谢!
代码:
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', 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(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