閑人壹のPython笔记.2021.08.02.总结代码两则
今天闷头捯饬出两段代码,路子有点野,请多多指点!
代码.1:2进制IP转10进制计算器
代码说明: 此代码只能将 2进制IP转化为 10进制IP,无其他功能,思路简单,只为练习,没有使用数据类型转换函数
待改进方面: 1、未对输入数据进行控制,代码只能识别 8 位因内的 “0" 或 ”1“,输入其它值就会报错。 2、代码中算法是根据输入值的位数进行计算的,所以无论输入多大的 8 位数,在计算过程中只会将每一位上的数值当作 “0" 或 ”1“ 进行计算。
代码如下:
while (1):
print("2进制IP转换为10进制计算方法:")
print("2进制IP由32位2进制码构成,每8位一组,由“0”或“1”构成,从高位到低位每个“1”依此代表10进制数 2 的 7 ~ 0 次方,“0”即是0,"
"将2进制数“1”所代表的10进制数相加就可以得到2进制数对应的10进制数.\n")
print("示例:")
print("2进制数: 1………1………1………1………1……1……1……1")
print(" ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓")
print("10进制数:128……64……32……16……8……4……2……1")
print(" 128 +64 +32 +16 + 8 +4 +2 +1 = 255")
print("---------------------------------------------------------")
print("2进制 to 10进制转换器:")
bin = input("请输入第 1 段 8 位二进制数:")
bin2 = bin.zfill(8)
print("2进制IP地址码:", bin2)
b1 = int(bin2[0])
b2 = int(bin2[1])
b3 = int(bin2[2])
b4 = int(bin2[3])
b5 = int(bin2[4])
b6 = int(bin2[5])
b7 = int(bin2[6])
b8 = int(bin2[7])
if b1 == 0:
b11 = 0
else:
b11 = 128
if b2 == 0:
b21 = 0
else:
b21 = 64
if b3 == 0:
b31 = 0
else:
b31 = 32
if b4 == 0:
b41 = 0
else:
b41 = 16
if b5 == 0:
b51 = 0
else:
b51 = 8
if b6 == 0:
b61 = 0
else:
b61 = 4
if b7 == 0:
b71 = 0
else:
b71 = 2
if b8 == 0:
b81 = 0
else:
b81 = 1
print("第1位 Bin.1 =", b1, "= 10进制数 Dec(", b11, ")")
print("第2位 Bin.2 =", b2, "= 10进制数 Dec(", b21, ")")
print("第3位 Bin.3 =", b3, "= 10进制数 Dec(", b31, ")")
print("第4位 Bin.4 =", b4, "= 10进制数 Dec(", b41, ")")
print("第5位 Bin.5 =", b5, "= 10进制数 Dec(", b51, ")")
print("第6位 Bin.6 =", b6, "= 10进制数 Dec(", b61, ")")
print("第7位 Bin.7 =", b7, "= 10进制数 Dec(", b71, ")")
print("第8位 Bin.8 =", b8, "= 10进制数 Dec(", b81, ")")
print("按照上述计算方法:")
print("10进制码 = Bin.1 + Bin.2 + Bin.3 + Bin.4 + Bin.5 + Bin.6 + Bin.7 + Bin.8")
print("即")
D1 = b11 + b21 + b31 + b41 + b51 + b61 + b71 + b81
print(b11, "+", b21, "+", b31, "+", b41, "+", b51, "+", b61, "+", b71, "+", b81, "=", D1)
print("10进制地址码位:", D1)
print("---------------------------------------------------------")
bin = input("请输入第 2 段 8 位二进制数:")
bin2 = bin.zfill(8)
print("2进制IP地址码:", bin2)
b1 = int(bin2[0])
b2 = int(bin2[1])
b3 = int(bin2[2])
b4 = int(bin2[3])
b5 = int(bin2[4])
b6 = int(bin2[5])
b7 = int(bin2[6])
b8 = int(bin2[7])
if b1 == 0:
b11 = 0
else:
b11 = 128
if b2 == 0:
b21 = 0
else:
b21 = 64
if b3 == 0:
b31 = 0
else:
b31 = 32
if b4 == 0:
b41 = 0
else:
b41 = 16
if b5 == 0:
b51 = 0
else:
b51 = 8
if b6 == 0:
b61 = 0
else:
b61 = 4
if b7 == 0:
b71 = 0
else:
b71 = 2
if b8 == 0:
b81 = 0
else:
b81 = 1
D2 = b11 + b21 + b31 + b41 + b51 + b61 + b71 + b81
print("10进制地址码位:", D2)
print("---------------------------------------------------------")
bin = input("请输入第 3 段 8 位二进制数:")
bin2 = bin.zfill(8)
print("2进制IP地址码:", bin2)
b1 = int(bin2[0])
b2 = int(bin2[1])
b3 = int(bin2[2])
b4 = int(bin2[3])
b5 = int(bin2[4])
b6 = int(bin2[5])
b7 = int(bin2[6])
b8 = int(bin2[7])
if b1 == 0:
b11 = 0
else:
b11 = 128
if b2 == 0:
b21 = 0
else:
b21 = 64
if b3 == 0:
b31 = 0
else:
b31 = 32
if b4 == 0:
b41 = 0
else:
b41 = 16
if b5 == 0:
b51 = 0
else:
b51 = 8
if b6 == 0:
b61 = 0
else:
b61 = 4
if b7 == 0:
b71 = 0
else:
b71 = 2
if b8 == 0:
b81 = 0
else:
b81 = 1
D3 = b11 + b21 + b31 + b41 + b51 + b61 + b71 + b81
print("10进制地址码位:", D3)
print("---------------------------------------------------------")
bin = input("请输入第 4 段 8 位二进制数:")
bin2 = bin.zfill(8)
print("2进制IP地址码:", bin2)
b1 = int(bin2[0])
b2 = int(bin2[1])
b3 = int(bin2[2])
b4 = int(bin2[3])
b5 = int(bin2[4])
b6 = int(bin2[5])
b7 = int(bin2[6])
b8 = int(bin2[7])
if b1 == 0:
b11 = 0
else:
b11 = 128
if b2 == 0:
b21 = 0
else:
b21 = 64
if b3 == 0:
b31 = 0
else:
b31 = 32
if b4 == 0:
b41 = 0
else:
b41 = 16
if b5 == 0:
b51 = 0
else:
b51 = 8
if b6 == 0:
b61 = 0
else:
b61 = 4
if b7 == 0:
b71 = 0
else:
b71 = 2
if b8 == 0:
b81 = 0
else:
b81 = 1
D4 = b11 + b21 + b31 + b41 + b51 + b61 + b71 + b81
print("10进制地址码位:", D4)
print("---------------------------------------------------------")
print("10进制IP地址为:")
print(D1,".",D2,".",D3,".",D4)
print("请记录")
print("---------------------------------------------------------")
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
代码.2:数字矩阵列举器
代码说明: 输入一个整数值,列举出所有 每行、每列和两条对角线上的三个数的和都为此值的数字排列组合
待改进方面: 1、未对输入数据进行控制,输入任何非整数值都会报错弹出。 2、算法太过直接,如何给出较大整数计算时间会很长。
代码如下:
while(1):
print("题目:填充下表使表中每行、每列和两条对角线上的三个数的和都为18")
print("_____________")
print("| a | b | c |")
print("—————————————")
print("| d | e | f |")
print("—————————————")
print("| g | h | i |")
print("—————————————")
print("")
he = int(input("计算前请先输入一个 3 的倍数作为行列对角的和,""\n""如题目 18,数值也大运算时间越长:"))
print("答案为:")
cishu = 0
for a in range(1, he + 1):
for e in range(1, he + 1):
for i in range(1, he + 1):
if a + e + i == he:
for c in range(1, he + 1):
for g in range(1, he + 1):
if c + e + g == he:
for d in range(1, he + 1):
for f in range(1, he + 1):
if d + e + f == he:
for b in range(1, he + 1):
for h in range(1, he + 1):
if b + e + h == he:
if a + b + c == he and d + e + f == he and g + h + i == he and a + d + g == he and b + e + h == he and c + f + i == he and a + e + i == he and c + e + g == he:
if cishu <= 10000:
cishu = cishu + 1
print("排序:", cishu)
print("_____________")
print("|", a, "|", b, "|", c, "|")
print("—————————————")
print("|", d, "|", e, "|", f, "|")
print("—————————————")
print("|", g, "|", h, "|", i, "|")
print("—————————————")
break
print("共", cishu, "种方案")
print("___________________________________")
print("閑人壹.Python 基础学习部分总结作品")
print("___________________________________")
总结
初学乍练,问题对对,请各位大神多多指点,万分感谢!
|