這裡使用了三個知識點:
1、函數的定義
2、使用者自定義輸入input
3、try-except子產品捕獲了錯誤,如果失敗向使用者顯示了一個自定義的錯誤消息
1
2
3
4
5
6
7
8
<code>def get_remote_sysinfo():</code>
<code> </code><code>print(</code><code>"Example:www.baidu.com"</code><code>)</code>
<code> </code><code>remote_host = input(</code><code>"Please input remot_host:"</code><code>)</code>
<code> </code><code>try:</code>
<code> </code><code>print(</code><code>"IP address: %s"</code> <code>%socket.gethostbyname(remote_host))</code>
<code> </code><code>except socket.error:</code>
<code> </code><code>print (</code><code>"Sorry!~ Don't Found %s"</code> <code>%remote_host)</code>
<code>get_remote_sysinfo()</code>
執行正确結果:
<a href="https://s2.51cto.com/wyfs02/M02/9E/7B/wKioL1mSXGyiOVA2AAA36oLFvv8467.jpg-wh_500x0-wm_3-wmp_4-s_4265879264.jpg" target="_blank"></a>
錯誤結果:
<a href="https://s1.51cto.com/wyfs02/M01/9E/7B/wKioL1mSXJPRcbchAAA1bGUMhps465.jpg-wh_500x0-wm_3-wmp_4-s_3028989303.jpg" target="_blank"></a>
注意事項:
函數定義:函數内代碼要縮進,不然會文法報錯,切記!~
本文轉自 yuri_cto 51CTO部落格,原文連結:http://blog.51cto.com/laobaiv1/1956322,如需轉載請自行聯系原作者