標籤:error inpu file 字串表 error: string line 運算式 traceback
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 的區別