這裏使用了三個知識點:
1、函數的定義
2、用戶自定義輸入input
3、try-except模塊捕獲了錯誤,如果失敗向用戶顯示了一個自定義的錯誤消息
def get_remote_sysinfo(): print("Example:www.baidu.com") remote_host = input("Please input remot_host:") try: print("IP address: %s" %socket.gethostbyname(remote_host)) except socket.error: print ("Sorry!~ Don't Found %s" %remote_host) get_remote_sysinfo()
執行正確結果:
錯誤結果:
注意事項:
函數定義:函數內代碼要縮進,不然會語法報錯,切記!~