Exercise_5
Exercise 5 : Primitive Types
Test :
int 整型
bool 布尔型
float 浮点型
str 字符串
因为计算机为二进制,所以True(1),False(0)
int() 向下取整(去除小数部分)
int(x + 0.5)实现对x的四舍五入
type() : 获得关于类型的信息 返回<class ‘ ‘>
isinstance() : 更建议使用,isinstance(待检验的, 类型),相同返回True,不同返回False
python3 支持uft—8编码故可以使用中文名命名变量
Try :
- 判断闰年
1 2 3 4 5 6 7 8 9 10 11
| year = input('Please enter a number of year : ') while not year.isdigit(): year = input('Pls enter an interger:')
year = int(year) if year % 400 == 0: print('闰年') elif year % 4 == 0 and year % 100 != 0: print('闰年') else: print('不是闰年')
|
1 2 3 4 5 6 7 8 9 10 11
| temp = input('Please enter a year:') while not temp.isdigit(): temp = input('Sorry, pls enter a interger:')
year = int(temp) if year/400 ==int(year/400): print(temp + '是闰年!') elif year/4 == int(year/4) and (year/100 != int(year/100)): print(temp + '是闰年!') else: print(temp + '不是闰年!')
|