Python 之 input 與 raw_input 的區別

來源:互聯網
上載者:User

標籤: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 的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.