Problem In CryptoCurrency code Not working

Hello guys,
Yesterday I tried to complete the cryptocurrency exercise. In the exercise, it was given that when the bitcoin price will be greater than the decided value buzzer should trigger on. But I tried many things but for some reason, it is not working.

when code is run it does not give any error just it stucks there and nothing happens.
so if anybody knows how to fix my problem I would really appreciate their help.

Hi @prajesh9921

The snap you have attached is not very clear. But you can try these 2 steps:

  1. Check you network connection, sometime due to slow speed api’s unable to capture the get request.
  2. check whether url is correct or not. This code might help:
    response = requests.request(“GET”, URL)
    response = json.loads(response.text)
    print(response)

I am, sending you full step . Pls follow this step you will get to know what step you missed before …

Login to your Ubuntu server and create a folder and you can give any name to the folder.

mkdir crypto_alert

After creating the folder we will go inside the folder by typing the below commands.

cd crypto_alert

now we shall install the pip package manager for Python-2.7. A pip command is a tool for installing and managing Python packages, such as those found in the Python Package Index, For example, boltiot, Twilio etc.

sudo apt-get -y install python-pip

and then we will install the python virtualenv and virtualenvwrapper.

virtualenv will help us to create isolated Python environments. Type the below command to install virtualenv and virtualenvwrapper.

sudo apt-get install python-virtualenv

sudo pip install virtualenvwrapper

After installing the virtualenv and virtualenvwrapper , we will add it in bashrc file.

sudo echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc 
sudo echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc 
source ~/.bashrc

Now installation is done and we will create a virtual environment.

mkvirtualenv crypto_alert

The above command will create virtual environment with name crypto_alert

and we will activate the crypto_alert virtual environment by typing the below command.

workon crypto_alert

and then we shall install the boltiot packages and after installing the boltiot packages.

pip install boltiot

pip install --upgrade pip

and we will install some security packages because we will be using some external API.

pip install pyOpenSSL ndg-httpsclient pyasn1

pip install 'requests[security]'
def get_bitcoin_price():
   URL = "https://min-api.cryptocompare.com/" # REPLACE WITH CORRECT URL
   response = requests.request("GET", URL)
   response = json.loads(response.text)
   current_price = response["USD"]
   return current_price

now at last use this code . hope your problem will solve now …

Hi @prajesh9921,

Your code is incorrect because you are defining the get_bitcoin_price() function inside the while loop.

  1. First, your get_bitcoin_price should be defined outside the while loop.
  2. Then you should call the get_bitcoin_price function inside the while loop to fetch the current price.

For example:

import requests, json

def get_bitcoin_price():
    URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD" # REPLACE WITH CORRECT URL
    response = requests.request("GET", URL)

    response = json.loads(response.text)

    print ("response is", response)
    current_price = response["USD"]
    return current_price

while True:
    print ("Inside while loop. Calling get_bitcoin_price function to fetch the price")
    price = get_bitcoin_price()
    print (price)

Do let me know in case you need further assistance.

Hi @rahul.singh1,
Thank you @rahul.singh1 for pointing out the mistake. My code now works perfectly thanks to you.

1 Like