簡體   English   中英

為什么我的Python函數無法執行?

[英]Why won't my Python function execute?

我是編碼新手。 我編寫了一段簡單的代碼,它將URL作為輸入,加載URL並將該URL的狀態代碼放入變量中。 我把它放在一個函數中。 但是,當我運行代碼時,我沒有收到任何錯誤,但該功能無法運行。 好像解釋器正在超越我的職能。 我知道我有一些簡單的錯誤,但是無論我搜索了多少我都無法解決。 請幫助我理解。 哦,代碼不完整。 但是該功能應該運行。

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): ")

您的功能肯定正在工作。 您面臨的問題是您正在從get_stat()返回一個值(這是return語句所做的事情),但您從未真正說過應該打印該值。

我的猜測是您要打印返回的值,在這種情況下,您需要添加print語句:

print(get_stat())

或者,您可以將值存儲為變量:

a = get_stat()
print(a)

如以下在quamrana的評論中所述,盡管以下方法被認為是不好的做法,但您可以將print()語句放入函數中以進行調試,並在以后替換它:

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

這將證明該功能確實正在執行。

這篇文章可能會幫助您更好地了解return語句的作用以及如何從中獲取值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM