1. 程式人生 > >Python 之 input 與 raw_input 的區別

Python 之 input 與 raw_input 的區別

you yellow str enter raw mos mod line 進行

input 接受合法的Python 表達式 raw_input 將所有的輸入作為原始數據,將其放入字符串中 >>> name = input("what‘s your name ?") what‘s your name ? Yellow Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> name = input("What‘s is your name ?") File "<string>", line 1, in <module> NameError: name ‘yellow‘ is not defined >>> Yellow = "yellow" >>> name = input("what‘s your name ?") what‘s your name ? Yellow >>> print "Hello, " + name Hello, yellow >>> input(‘Enter a number ‘) Enter a number 3 3 第一次輸入“Yellow”時,作為表達式,但是沒有定義,故報錯,如果輸入為正確表達式,則需要加單引號或雙引號,作為字符串表達式輸入。 第二次定義了Yellow為表達式,並進行了賦值操作,所以再次輸入,為合法的表達式,故沒有報錯。 第三次輸入數字3,作為數字,即合法的表達式,故沒有報錯。 raw_input() 函數將所有輸入原始數據,並放入字符串中,故不會報錯。 >>> name = raw_input("What‘s is your name ?") What‘s is your name ?Yellow >>> print "Hello, " + name Hello, Yellow

Python 之 input 與 raw_input 的區別