Hi I'm currently making a Bitcoin Price tracker. I have it rigged up so that it will send me an alert when Bitcoins price hits a certain point. I'm still beginner programmer, but it's working good so far.
I'm having trouble figuring out how I should scope and then call my two functions without the function calling itself over and over. The variable I need both functions to have access to is 'current_price'. What is the correct way to scope/organize then call these these so that my inner function can access my outer functions variables? Thank you!
The code:
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()
There are two ways to do this. Global variables, often frowned upon and passing in current_price as an argument.
Global Variables - frowned upon:
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()
and passing in 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
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