Why is PyCharm saying a section of my code within a funciton is unreachable, despite it is working as intended?

Viewed 31

I have this code that checks internet connection and sets a boolean accordingly. I've put the code in a function. But PyCharm is showing that this line: "start_time = time.time()" is unreachable. Did I write my code wrong?

Screenshot: pic of warning

The code:

from time import time, sleep
import os


def something():
    while True:
        sleep(2 - time() % 2)
        ip_list = ['8.8.8.8']
        for ip in ip_list:
            response = os.popen(f"ping {ip}").read()
            if "Received = 4" in response:
                print(f"UP {ip} Ping Successful")
                _connected = True
                print(_connected)
            else:
                print(f"DOWN {ip} Ping Unsuccessful")
                _connected = False
                print(_connected)


    start_time = time.time()

    seconds = 1

    while True:
        current_time = time.time()
        elapsed_time = current_time - start_time

        if elapsed_time > seconds:
            print("Finished iterating in: " + str(int(elapsed_time)) + " seconds")
            break


something()

My end goal here is to understand why PyCharm would call this warning. It seems to me that everything is correct.

1 Answers

I don't believe you have any way to break out of your while loop. You probably need a break statement in there.

Related