首頁  >  文章  >  後端開發  >  python:中input()與raw_input()的詳解

python:中input()與raw_input()的詳解

黄舟
黄舟原創
2017-10-07 11:40:331660瀏覽


實驗

a = input('请输入:')
print a

如果輸入字串,則馬上報錯:

请输入:str  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>

但是如果輸入整數,則不會報錯:

请输入:1010

如果把input 改成raw_input ,則可以正常記錄鍵盤輸入的字串:

a = raw_input(&#39;请输入:&#39;)print a
请输入:str
str

原因

原因就在於, input 只能接受整數輸入:

a = input(&#39;请输入:&#39;)print type(a)
请输入:10<type &#39;int&#39;>

raw_input 可以接受字串輸入:

a = raw_input(&#39;请输入:&#39;)print type(a)
请输入:str
<type &#39;str&#39;>

#

以上是python:中input()與raw_input()的詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn