0

Super simple not complex and don't need it to be. Just wanted to add the current prices of crypto and my equity of these prices as well as a total to display on screen. I want these values to refresh in place so they don't create new lines every time it refreshes.

Here is what I have:

import requests
import math
from datetime import datetime

URL = "https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}"

def get_price(coin, currency):
    try:
        response = requests.get(URL.format(coin, currency)).json()
        return response
    except:
        return False

while True:
    date_time = datetime.now()
    date_time = date_time.strftime("%m/%d/%Y %H:%M:%S")
    currentPrice = get_price("ETH", "USD")
    totalPrice = currentPrice["USD"]*2
    currentPrice0 = get_price("DOGE", "USD")
    totalPrice0 = currentPrice0["USD"]*100
    currentPrice1 = get_price("ADA", "USD")
    totalPrice1 = currentPrice1["USD"]*20
    currentPrice2 = get_price("SOL", "USD")
    totalPrice2 = currentPrice2["USD"]*4
    currentPrice3 = (totalPrice + totalPrice0 + totalPrice1 + totalPrice2)
        
    print("ETH  | PRICE: $", currentPrice["USD"],  " EQUITY: $", totalPrice)
    print("DOGE | PRICE: $", currentPrice0["USD"], " EQUITY: $", totalPrice0)
    print("ADA  | PRICE: $", currentPrice1["USD"], " EQUITY: $", totalPrice1)
    print("SOL  | PRICE: $", currentPrice2["USD"], " EQUITY: $", totalPrice2)
    print(" ")
    print("TOTAL: ", currentPrice3)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You will need to output [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) in order to control the cursor location when printing to the console/terminal (assuming it understand them). While it can be done manually most folks using a module like [`curses`](https://docs.python.org/3/library/curses.html#module-curses) to simplify the task or a third-party module for non-Unixy OSs. – martineau Sep 08 '21 at 00:28
  • I agree with @martineau on this. Plus I would advise to change what you return of `get_price` in case of error: if you return `False`, you'll have an error when you try to get the value, like `currentPrice["USD"]`. I'd recommend returning an empty dict instead (`{}`) and using [`.get('...', 0)`](https://docs.python.org/3/library/stdtypes.html#dict.get). – bastantoine Sep 08 '21 at 07:53
  • See the HOWTO: [Curses Programming with Python](https://docs.python.org/3/howto/curses.html) in the documentation. – martineau Sep 08 '21 at 22:33

0 Answers0