小編給大家分享一下python定義用戶輸入的方法,相信大部分人都還不怎么了解,因此分享這邊文章給大家學(xué)習(xí),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去學(xué)習(xí)方法吧!
創(chuàng)新互聯(lián)建站一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、移動互聯(lián)產(chǎn)品、成都全網(wǎng)營銷推廣服務(wù)為核心業(yè)務(wù)。10余年網(wǎng)站制作的經(jīng)驗(yàn),使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
Python中獲取用戶輸入的方法如下:
整數(shù)輸入
#!/usr/bin/python3 usr_ip = input("Enter an integer number: ")
# 需要將輸入的字符串顯式地指定為需要的類型
usr_num = int(usr_ip) sqr_num = usr_num * usr_num print("Square of entered number is: {}".format(sqr_num))
讓我們給定一個整數(shù)和字符串來測定這個程序
Python docs - 整數(shù)文本
$ ./user_input_int.py Enter an integer number: 23 Square of entered number is: 529 $ ./user_input_int.py Enter an integer number: abc Traceback (most recent call last): File "./user_input_int.py", line 6, in <module> usr_num = int(usr_ip) ValueError: invalid literal for int() with base 10: 'abc'
浮點(diǎn)數(shù)輸入
#!/usr/bin/python3 usr_ip = input("Enter a floating point number: ") # 需要將輸入的字符串顯式地指定為我們需要的類型 usr_num = float(usr_ip) sqr_num = usr_num * usr_num # 限制小數(shù)點(diǎn)位數(shù) print("Square of entered number is: {0:.2f}".format(sqr_num))
E 科學(xué)計(jì)數(shù)法在需要時可以使用
Python文檔 - 浮點(diǎn)數(shù)文本
Python文檔 - 浮點(diǎn)數(shù)
$ ./user_input_float.py Enter a floating point number: 3.232 Square of entered number is: 10.45 $ ./user_input_float.py Enter a floating point number: 42.7e5 Square of entered number is: 18232900000000.00 $ ./user_input_float.py Enter a floating point number: abc Traceback (most recent call last): File "./user_input_float.py", line 6, in <module> usr_num = float(usr_ip) ValueError: could not convert string to float: 'abc'
字符串輸入
#!/usr/bin/python3 usr_name = input("Hi there! What's your name? ") usr_color = input("And your favorite color is? ") print("{}, I like the {} color too".format(usr_name, usr_color))
不像Perl,字符串輸入不需要進(jìn)行類型轉(zhuǎn)換和注意換行符
$ ./user_input_str.py Hi there! What's your name? learnbyexample And your favorite color is? blue learnbyexample, I like the blue color too
以上是python定義用戶輸入的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:python定義用戶輸入的方法
轉(zhuǎn)載來源:http://muchs.cn/article40/piheho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、響應(yīng)式網(wǎng)站、網(wǎng)站收錄、標(biāo)簽優(yōu)化、微信公眾號、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)