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): ")
asked Feb 3, 2018 at 13:03
3
  • You execute the function, but you don't do anything with the value it returns. Maybe you mean print(get_stat())? Commented Feb 3, 2018 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? Commented Feb 3, 2018 at 13:06
  • Yes. I see that now. Thanks guys. I really couldn't figure it out. First time working with functions! Commented Feb 3, 2018 at 13:16

1 Answer 1

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.

answered Feb 3, 2018 at 13:11
Sign up to request clarification or add additional context in comments.

3 Comments

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 I did not know that. Thank you for correcting me. Please see my edited post.
Thank you very much Simon. This is a very helpful answer and solved my problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.