Python中input與raw_input 之間的比較
input和raw_input均可以接收輸入,其差別如下所示:
#input假設用戶輸入的是合法的Python表達式>>> name = input("what is your name?")what is your name?ZJTraceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module>NameError: name 'ZJ' is not defined#于是,必須這么使用,輸入Python格式的字符串"ZJ">>> name = input("what is your name?")what is your name? "ZJ">>> print nameZJ>>> #raw_input會把所有輸入當作原始數據(raw data),然后將其放入字符串中>>> name = raw_input("what is your name?")what is your name?ZJ>>> print nameZJ>>>因此,一般情況下應盡可能的使用raw_input。
input 輸入
用法1: input(“提示信息/亦可空缺”); 結果為 雙引號內字符串;不保存 只輸出后續填寫信息。Python2 使用為表達式,若為數字計算符則會計算輸出結果。
用法2: 變量 = input(“提示信息/亦可空缺”); 結果為 不顯示字符,后續填寫信息保存在變量當中 輸入變量運行顯示才輸入信息。
raw_input輸入
用法 raw_input(“提示信息/亦可空缺”);結果為 雙引號內字符串;不保存 只輸出后續填寫信息。 Python2 使用 等同于python3中 input (輸入信息原封不動的顯示)。
以上就是 Python中input與raw_input 之間的比較,有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答