Repository navigation
Expand file tree
/
Copy pathpython_test02_bit
More file actions
130 lines (121 loc) · 3.01 KB
/
Copy pathpython_test02_bit
File metadata and controls
130 lines (121 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#练习02:给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字
#code01:最初始的实现
num = int(input("please input num: "))
ext = 0
if num >= 100:
if num >= 100 and num < 1000:
ext = 3
print("The num is 3 bit")
elif num >= 1000 and num < 10000:
ext = 4
print("The num is 4 bit")
elif num >= 10000 and num < 100000:
ext = 5
print("The num is 5 bit")
else:
if num >= 10:
ext = 2
print("The num is 2 bit")
elif num > 0 and num < 10:
ext = 1
print("The num is 1 bit")
k = 1
s = 0 #每位数
#m = 0
print(str(num) + " all kinds of:")
for i in range(ext):
k = 10
s = num % k
print(s)
num = num // k
#code02:添加校验和输出格式
num = int(input("please input num: "))
ext = 0 #数位
if num > 0 and num < 100000:
if num >= 100:
if num >= 100 and num < 1000:
ext = 3
print("The num is 3 bit")
elif num >= 1000 and num < 10000:
ext = 4
print("The num is 4 bit")
elif num >= 10000 and num < 100000:
ext = 5
print("The num is 5 bit")
else:
if num >= 10:
ext = 2
print("The num is 2 bit")
elif num > 0 and num < 10:
ext = 1
print("The num is 1 bit")
s = 0 #每一位的值
print(str(num) + " all kinds of:")
for i in range(ext):
k = 10
s = num % k
if i == 0:
print("个位: " + str(s))
elif i == 1:
print("十位: " + str(s))
elif i == 2:
print("百位: " + str(s))
elif i == 3:
print("千位: " + str(s))
else:
print("万位: " + str(s))
num = num // k
else:
print("input is error,please enter num!")
#code03:通过循环和break语句的使用,精简
num = int(input("please input num: "))
ext = 0 #数位
if num > 0 and num < 100000:
while True:
print(num%10)
ext += 1
num = num // 10
if num == 0:
break
print("这个数的位数是: ",ext)
else:
print("input is error,please enter num!")
#code04:break循环
num = int(input("input a 5bit num : "))
txt = 0
#计数器
if num >= 1000:
if num < 10000:
txt = 4
print("num is a 4 bit")
elif num < 100000:
txt = 5
print("num is a 5 bit")
else:
if num >= 100:
txt = 3
print("num is a 3 bit")
elif num >= 10:
txt = 2
print("num is a 2 bit")
else:
txt = 1
print("num is a 1 bit")
while True:
if txt >= 1:
print(num % 10)
num = num // 10
txt = txt - 1
else:
break
#code05:break循环
num = int(input("input a 5bit num : "))
debain = 0
while True:
if num > 0:
print(num % 10)
num = num // 10
debain = debain + 1 #统计位数
else:
break
print("num is a ",debain,"bit")