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:
JavaScript
x
47
47
1
import smtplib
2
import os
3
import requests
4
import json
5
6
email_user = os.environ.get('EMAIL_USER')
7
email_password = os.environ.get('APP_PASS')
8
phone_number = os.environ.get('USER_PHONE')
9
10
target_price = 20000 # set your target price in USD
11
12
def send_email():
13
14
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
15
smtp.ehlo()
16
smtp.starttls()
17
smtp.ehlo()
18
19
smtp.login(email_user, email_password)
20
21
subject = 'Bitcoin Alert: '
22
body = 'Bitcoins price is ' + str(target_price) + "!"
23
24
msg = f'Subject: {subject}nn{body}'
25
26
smtp.sendmail(email_user, phone_number, msg)
27
28
def check_price():
29
30
response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
31
data = response.json()
32
33
currency = data["data"]["base"]
34
current_price = data["data"]["amount"]
35
current_price = int(float(current_price)) ## converts string float to int
36
37
if current_price > target_price:
38
send_email()
39
40
print(f'Bitcoins current price is {current_price}')
41
42
43
check_price()
44
45
46
47
Advertisement
Answer
There are two ways to do this. Global variables, often frowned upon and passing in current_price as an argument.
Global Variables – frowned upon:
JavaScript
1
44
44
1
import smtplib
2
import os
3
import requests
4
import json
5
6
email_user = os.environ.get('EMAIL_USER')
7
email_password = os.environ.get('APP_PASS')
8
phone_number = os.environ.get('USER_PHONE')
9
10
target_price = 20000 # set your target price in USD
11
12
def send_email():
13
14
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
15
smtp.ehlo()
16
smtp.starttls()
17
smtp.ehlo()
18
19
smtp.login(email_user, email_password)
20
21
subject = 'Bitcoin Alert: '
22
body = 'Bitcoins price is ' + str(target_price) + "!"
23
24
msg = f'Subject: {subject}nn{body}'
25
26
smtp.sendmail(email_user, phone_number, msg)
27
28
def check_price():
29
global current_price #global variable declared
30
response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
31
data = response.json()
32
33
currency = data["data"]["base"]
34
current_price = data["data"]["amount"]
35
current_price = int(float(current_price)) ## converts string float to int
36
37
if current_price > target_price:
38
send_email()
39
40
print(f'Bitcoins current price is {current_price}')
41
42
43
check_price()
44
and passing in current price:
JavaScript
1
46
46
1
import smtplib
2
import os
3
import requests
4
import json
5
6
email_user = os.environ.get('EMAIL_USER')
7
email_password = os.environ.get('APP_PASS')
8
phone_number = os.environ.get('USER_PHONE')
9
10
target_price = 20000 # set your target price in USD
11
12
current_price = 0 # declare current price
13
14
def send_email(current_price):
15
16
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
17
smtp.ehlo()
18
smtp.starttls()
19
smtp.ehlo()
20
21
smtp.login(email_user, email_password)
22
23
subject = 'Bitcoin Alert: '
24
body = 'Bitcoins price is ' + str(target_price) + "!"
25
26
msg = f'Subject: {subject}nn{body}'
27
28
smtp.sendmail(email_user, phone_number, msg)
29
30
def check_price(current_price):
31
32
response = requests.get("https://api.coinbase.com/v2/prices/BTC-USD/spot")
33
data = response.json()
34
35
currency = data["data"]["base"]
36
current_price = data["data"]["amount"]
37
current_price = int(float(current_price)) ## converts string float to int
38
39
if current_price > target_price:
40
send_email(current_price)
41
42
print(f'Bitcoins current price is {current_price}')
43
44
45
check_price(current_price) #current price is passed in
46