Python採用raw_input讀取輸入值的方法

來源:互聯網
上載者:User
本文較為詳細的介紹了python中raw_input的用法,使用raw_input 能夠很方便的叢控制台讀入資料。具體用法樣本如下:

1.輸入字串

#13222319810101****nID = ''while 1:  nID = raw_input("Input your id plz")  if len(nID) != len("13222319810101****"):    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 %d\n' % nAge

3.輸入浮點型

fWeight = 0.0fWeight = float(raw_input("input your weight\n"))print 'your weight is %f' % fWeight

4.輸入16進位資料

nHex = int(raw_input('input hex value(like 0x20):\n'),16)print 'nHex = %x,nOct = %d\n' %(nHex,nHex)

5.輸入8進位資料

nOct = int(raw_input('input oct value(like 020):\n'),8)print 'nOct = %o,nDec = %d\n' % (nOct,nOct)

本文樣本對Python初學者有一定的學習借鑒價值,感興趣的讀者可以動手調試運行一下本文樣本,以加深對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.