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

- 上一篇
python如何創(chuàng)建txt文件并寫入
簡介python創(chuàng)建txt文件并寫入的方法:首先開始寫【#!/usr/bin/python】,并設定編碼格式;然后建立txt文檔,并在里面寫文字;最后打開文檔即可。本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELLG3電腦。python創(chuàng)建txt文件并寫入的方法:1、打開python的
- 下一篇
python中format怎么用
簡介python中format的使用方法:【format():】把傳統(tǒng)的【%】替換為【{}】來實現(xiàn)格式化輸出,代碼為【'數(shù)字{1}{2}和{0}'.format(123,456,'789')】。本教程操作環(huán)境:windows7系統(tǒng)、python3.