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