資訊內容
python如何判斷整數
python判斷整數的方法:1、可以使用字符串str的is digit方法判斷字符串是否是一個僅有數字組成;2、可以使用【try-except】語句捕獲ValueError,并繼續請求輸入。G3z少兒編程網-https://www.pxcodes.com
G3z少兒編程網-https://www.pxcodes.com
本教程操作環境:windows7系統、python3.9版,DELL G3電腦。G3z少兒編程網-https://www.pxcodes.com
python判斷整數的方法:
G3z少兒編程網-https://www.pxcodes.com
1、可以使用字符串str的is digit方法判斷字符串是否是一個僅有數字組成,也就是整數。如果是整數退出while循環,否則繼續請求輸入。G3z少兒編程網-https://www.pxcodes.com
while True: x = input('Input an integer: ') if x.isdigit(): break else: print 'Please input an *integer*'2、也可以使用try-except語句。如果輸入的字符串是整數,那么它可以用用int()函數,轉換為int類并退出循環,否則會出現ValueError,可以使用try-except語句捕獲ValueError,然后繼續請求輸入。G3z少兒編程網-https://www.pxcodes.com
while True: try: x = input('Input an integer: ') x = int(x) break except ValueError: print 'Please input an *integer*'相關免費學習推薦:python視頻教程G3z少兒編程網-https://www.pxcodes.com
以上就是python如何判斷整數的詳細內容,更多請關注少兒編程網其它相關文章!G3z少兒編程網-https://www.pxcodes.com

- 上一篇
一起看看python常用字符串及其操作
簡介python常用字符串我們在學習python的時候會有一些常用的字符串,我將其做一些整理,實際上還有很多,這邊只是舉例。1.eval(str)print(12+3)print(eval(12+3))我們平常在print里面輸入的字符串都是直接輸出的,而
- 下一篇
python中階乘怎么表示
簡介python中階乘的表示方法:首先用def代碼創建函數,并創建一個變量res;然后寫入forrange循環,并在for循環當中進行計算并且返回res;最后用print代碼打印輸出3的階乘。本教程操作環境:windows7系統、python3.9版,DELLG3電腦。python中階乘的表示方法:1、