Exercise_5

Exercise 5 : Primitive Types

Test :

  1. int 整型

    bool 布尔型

    float 浮点型

    str 字符串

  2. 因为计算机为二进制,所以True(1),False(0)

  3. int() 向下取整(去除小数部分)

  4. int(x + 0.5)实现对x的四舍五入

  5. type() : 获得关于类型的信息 返回<class ‘ ‘>

    isinstance() : 更建议使用,isinstance(待检验的, 类型),相同返回True,不同返回False

  6. python3 支持uft—8编码故可以使用中文名命名变量

Try :

  1. 判断闰年
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 + '不是闰年!')

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!