【发布时间】:2020-11-06 19:52:08
【问题描述】:
我希望它根据波兰字母表和使用的符号计算熵:
- 如果只有大写或小写字母 32
- 如果它们对于 64 来说既小又大
- 如果有数字+10
- 如果有特殊字符+33
不幸的是,这些条件的第二个公式对我不起作用。当我输入“Apple”时,它会弹出 NameError: 名称 'entropia2' 未定义
import math
def entropy_poland(n):
print("Znaki nie powtarzają się, więc liczymy ze wzoru Hartleya: ")
if count_upper == True and count_lower == False and count_other == False and count_number == False:
entropy2 = math.log2(32)
elif count_upper == False and count_lower == True and count_other == False and count_number == False:
entropy2 = math.log2(32)
elif count_upper == False and count_lower == False and count_other == False and count_number == False:
entropy2 = math.log2(64)
elif count_upper == True and count_lower == False and count_other == False and count_number == True:
entropy2 = math.log2(42)
elif count_upper == False and count_lower == True and count_other == False and count_number == True:
entropy2 = math.log2(42)
elif count_upper == False and count_lower == False and count_other == False and count_number == True:
entropy2 = math.log2(74)
elif count_upper == False and count_lower == True and count_other == True and count_number == False:
entropy2 = math.log2(65)
elif count_upper == True and count_lower == False and count_other == True and count_number == False:
entropy2 = math.log2(65)
elif count_upper == False and count_lower == False and count_other == True and count_number == False:
entropy2 = math.log2(97)
elif count_upper == True and count_lower == False and count_other == True and count_number == True:
entropy2 = math.log2(75)
elif count_upper == False and count_lower == True and count_other == True and count_number == True:
entropy2 = math.log2(75)
elif count_upper == False and count_lower == False and count_other == True and count_number == True:
entropy2 = math.log2(107)
return entropy2
count_number = False
count_upper = False
count_lower = False
count_other = False
odp = "Reks"
for ascii in odp:
if chr(32) <= ascii <= chr(47) or chr(58) <= ascii <= chr(64) or chr(91) <= ascii <= chr(96) or chr(
123) <= ascii <= chr(126):
count_other = True
if chr(48) <= ascii <= chr(57):
count_number = True
if chr(65) <= ascii <= chr(90):
count_upper = True
if chr(97) <= ascii <= chr(122):
count_lower = True
print(entropy_poland(odp))
````
【问题讨论】:
-
我无法复制错误。请发布minimal reproducible example。由于您发布的代码从未尝试使用变量
entropia2,因此它无法抛出该错误。在else子句中的大多数地方你使用entropy2但在一个地方你使用entropia2似乎很奇怪。 -
我试图最小化代码,但我忘了改变熵2