python 研究-raw_input 和 input 差異比較

python中raw_input和input用法與區別
raw_input和input兩個函數是python中常用的函數了,但是我用到最多的是input函數了這個不有置疑了,但raw_input函數有與input函數在用法上有什麼區別呢?下面我們一起來看看吧。

在使用python編寫交互式程式時,經常用到的兩個內部函數是raw_input和input(最常用還是input) ,本篇就通過一些示例看下兩者之間的區別 。

一、raw_input
1.輸入字串
程式碼如下   
nID = ''
while 1:
nID = raw_input("Input your id plz")
if len(nID) != len("yang"):
print 'wring length of id,input again'
else:
break
print 'your id is %s' % (nID)

2.輸入整數
程式碼如下   
nAge = int(raw_input("input your age plz:n"))
if nAge > 0 and nAge < 120:
print 'thanks!'
else:
print 'bad age'
print 'your age is %dn' % nAge

3.輸入浮點型
程式碼如下   
fWeight = 0.0
fWeight = float(raw_input("input your weightn"))
print 'your weight is %f' % fWeight

4.輸入16進制資料
程式碼如下   
nHex = int(raw_input('input hex value(like 0x20):n'),16)
print 'nHex = %x,nOct = %dn' %(nHex,nHex)

5.輸入8進制資料
程式碼如下   

nOct = int(raw_input('input oct value(like 020):n'),8)
print 'nOct = %o,nDec = %dn' % (nOct,nOct)
raw_input,預設是返回的類別型是字串型,如果想要返回其他類別型的資料時,需要在語句前加上相應的資料類別型(如int 、float)。

二、input

input其實是通過raw_input來實現的,具體可以看python input的文件,原理很簡單,就下面一行程式碼:
程式碼如下   
def input(prompt):
return (eval(raw_input(prompt)))

再看一個示例:
程式碼如下   
#!/usr/bin/python
# -*- coding: utf-8 -*-
# guess.py
import random
guessesTaken = 0
print ('Hello! what is your name')
#myName = raw_input()
myName = input()
number = random.randint(1, 23)
print ('well, ' + myName + ', i am thinking of a number between 1 and 23.')
while guessesTaken < 7:
print ('Take a guess')
guess = input()
#guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print ('your guess is too low')
if guess > number:
print ('your guess is too high')
if guess == number:
break
if guess == number:
print ('good job! you guess in %d guesses!' %guessesTaken)
if guess != number:
print ('sorry! your guess is wrong')

上面的程式在執行時,無論使用數字或字串,執行第10行的資料輸入時,如果不加引號時會報錯。執行過程如下:

使用int數字型
程式碼如下   
# python guess.py
Hello! what is your name
111
Traceback (most recent call last):
File "guess.py", line 12, in ?
print ('well, ' + myName + ', i am thinking of a number between 1 and 23.')
TypeError: cannot concatenate 'str' and 'int' objects
使用不加引號的string
# python guess.py
Hello! what is your name
yang
Traceback (most recent call last):
File "guess.py", line 10, in ?
myName = input()
File "<string>", line 0, in ?
NameError: name 'yang' is not defined
使用加引號的string
# python guess.py
Hello! what is your name
"yang"
well, yang, i am thinking of a number between 1 and 23.
Take a guess
10
your guess is too low
Take a guess
15
good job! you guess in 2 guesses!

三、raw_input與input的區別

當輸入為純數字時

input返回的是數值類別型,如int,float
raw_inpout返回的是字串類別型,string類別型
當輸入字串時, input會計算在字串中的數字表達式,而raw_input不會。

如輸入 「57 + 3」:

input會得到整數60
raw_input會得到字串」57 + 3」

在版本2和版本3中的區別
程式碼如下   

<span style=";">Python 2.3.4 (#1, Feb 2 2005, 11:44:13)
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:")
please input:wei # raw_input 輸入 字串 成功
>>> user
'wei'
>>> user=input("please input:")
please input:123 # input 輸入 數字 成功(返回的是數字)
>>> user
123
>>> user=raw_input("please input:")
please input:111 <span style="white-space:pre"> </span> # raw_input 輸入 數字 成功(返回的還是當成字串)
>>> user
'111'
>>> user=input("please input:")
please input:wei # input 輸入字串 失敗
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
NameError: name 'wei' is not defined</span>

Python 2.7.3 同上

在Python 3.2.3中 input和raw_input 整合了,沒有了raw_input
程式碼如下   

<span style=";">Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:") #沒有了raw_input
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> user=input("please input:")
please input:wei
>>> user
'wei'
>>> user=input("please input:") #input的輸出結果都是作為字串
please input:123
>>> user
'123'</span>

Database error: [Duplicate entry 'python中raw input' for key 'term_title']

INSERT INTO ac_search_terms VALUES(NULL, 'python中raw input', 'pythonzhongrawinput',

留言

這個網誌中的熱門文章

c語言-關於#define用法

PHP教學 - 資料型態(Data Type) - 上

CMD常用網管指令