r/learnpython • • Apr 27 '21

I finally built something!

I am so freaking pumped right now. I finally built something that I did not copy directly from a video tutorial and it feels freaking amazing!

This is just a simple program that pulls data from the International Space Station API Where the ISS at? and it tells you the Altitude, Longitude, Latitude and Velocity (mph) of ISS in 3 second intervals.

There is a bit more I would like to do like add a timestamp but this is my first real program I've put together myself.

Any thoughts on what/where to improve?

import requests
import time


def get_iss_data():
    iss_api = 'https://api.wheretheiss.at/v1/satellites/25544'
    request = requests.get(iss_api).json()
    iss_latitude = request['latitude']
    iss_longitude = request['longitude']
    iss_altitude = request['altitude']
    iss_velocity = request['velocity']

    print(f'''
---------------------------
Real-time ISS Location:
---------------------------
Altitude: {iss_altitude}
Latitude: {iss_latitude}
Longitude: {iss_longitude}
Velocity MPH: {iss_velocity}
---------------------------
    ''')


def iss_current_data():
    i = 1
    while i <= 5:
        get_iss_data()
        time.sleep(3)
        i += 1


iss_current_data()
546 Upvotes

41 comments sorted by

View all comments

69

u/mfb1274 Apr 27 '21

You could just make that while loop into a for loop since you know you want 5.

for i in range(1,6):

Or better yet parameterize the function to pass in a upper bound there and then you’ll have the power to easily call different numbers. If you don’t know what hard coding is, look into it and get into good practices now

21

u/pconwell pconwell Apr 27 '21

Why not do for _ in range (5)? i is not doing anything here except incrementing the counter, so no need for range(1,6) as we don't care if we start at 1.

17

u/mfb1274 Apr 27 '21

That is very true, I wrote it like that in case any beginners weren’t aware the max parameter in range in non-inclusive. I always forgot that fact in the beginning

5

u/pconwell pconwell Apr 28 '21

Good point. I can see both sides of the argument here. Personally, I'd argue showing the "right" way with an explanation.