1

I'm quite new to coding. I've wrote a simple piece of code which will take a URL as an input, load the URL and put the status code for that URL in a variable. I have put this in a function. But when I run the code I get no errors and yet the function won't run. It seems like the interpreter is just flying past my function. I know I have some simple error but no matter how much I search I can't fix it. Please help me understand. Oh, and the code is incomplete. But the function should run.

import urllib.request, click, threading

url = input("Enter domain name >> \n")

def get_stat():
    status = urllib.request.urlopen("http://"+url).getcode()
    if status == 200:
        return "Site loads normally!\nHost Said: '%s'!" %(str(status))
    else:
        return "Site Needs To Be Checked!\nHost Said: '%s'!" %(str(status))
get_stat()

if click.confirm("Would you like to set a uptime watch?"):
    click.echo("You just confirmed something!!")
    count = input("Enter the number of times you wish your site to be checked: ")
    interval = input("Enter the time interval for status requests (Time is in minutes): ")
Saman Hamidi
  • 353
  • 1
  • 7
  • 20
  • You execute the function, but you don't do anything with the value it returns. Maybe you mean `print(get_stat())`? – Thierry Lathuille Feb 03 '18 at 13:05
  • 2
    How did you determine that your function didn't run? I just tried your code, and the function definitely *was* executed. What *precise* method did you use to determine the fact that the function didn't run? – Jörg W Mittag Feb 03 '18 at 13:06
  • Yes. I see that now. Thanks guys. I really couldn't figure it out. First time working with functions! – Saman Hamidi Feb 03 '18 at 13:16

1 Answers1

5

You function certainly is working. The problem you are facing is that you are returning a value from get_stat() (this is what the return statement does) but you never actually say that this value should print.

My guess is you want to print the values that are returned in which case you need to add the print statement:

print(get_stat())

Or you could store what the value as a variable:

a = get_stat()
print(a)

As said in quamrana's comment below although the following method is considered bad practice you can put the print() statement inside your function for debugging purposes and replace it later:

if status == 200:
    print("Site loads normally!\nHost Said: '%s'!" %(str(status)))
else:
    print("Site Needs To Be Checked!\nHost Said: '%s'!" %(str(status)))

This will prove that the function is indeed being executed.

This post might help you understand a little bit better what the return statement does and how you can get values from it.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 2
    Any of the above suggestions are good for debugging and they show that the function *does* indeed run. Note that performing a `print()` within the function is normally a bad idea and they should be removed once the problem is identified. – quamrana Feb 03 '18 at 13:24
  • @quamrana I did not know that. Thank you for correcting me. Please see my edited post. – Xantium Feb 03 '18 at 13:29
  • Thank you very much Simon. This is a very helpful answer and solved my problem. – Saman Hamidi Feb 03 '18 at 13:34