【发布时间】:2020-04-20 10:53:41
【问题描述】:
这是一个密码生成器,我无法确定问题出在哪里,但从输出来看,我可以说它在 turnFromAlphabet() 附近
函数turnFromAlphabet() 将字母字符转换为其整数值。
random 模块,我认为这里没有做任何事情,因为它只是决定将字符串中的字符转换为大写还是小写。如果一个字符串在其中一个,当发送或传递给turnFromAlphabet()时,它首先被转换为小写以避免错误,但仍然有错误。
代码:
import random
import re
#variables
username = "oogisjab" #i defined it already for question purposes
index = 0
upperOrLower = []
finalRes = []
index2a = 0
#make decisions
for x in range(len(username)):
decision = random.randint(0,1)
if(decision is 0):
upperOrLower.append(True)
else:
upperOrLower.append(False)
#Apply decisions
for i in range(len(username)):
if(upperOrLower[index]):
finalRes.append(username[index].lower())
else:
finalRes.append(username[index].upper())
index+=1
s = ""
#lowkey final
s = s.join(finalRes)
#reset index to 0
index = 0
def enc(that):
if(that is "a"):
return "@"
elif(that is "A"):
return "4"
elif(that is "O"):
return "0" #zero
elif(that is " "):
# reduce oof hackedt
decision2 = random.randint(0,1)
if(decision2 is 0):
return "!"
else:
return "_"
elif(that is "E"):
return "3"
else:
return that
secondVal = []
for y in range(len(s)):
secondVal.append(enc(s[index]))
index += 1
def turnFromAlphabet(that, index2a):
alp = "abcdefghijklmnopqrstuvwxyz"
alp2 = list(alp)
for x in alp2:
if(str(that.lower()) == str(x)):
return index2a+1
break
else:
index2a += 1
else:
return "Error: Input is not in the alphabet"
#real final
finalOutput = "".join(secondVal)
#calculate some numbers and chars from a substring
amount = len(finalOutput) - round(len(finalOutput)/3)
getSubstr = finalOutput[-(amount):]
index = 0
allFactors = {
};
#loop from substring
for x in range(len(getSubstr)):
hrhe = re.sub(r'\d', 'a', ''.join(e for e in getSubstr[index] if e.isalnum())).replace(" ", "a").lower()
print(hrhe)
#print(str(turnFromAlphabet("a", 0)) + "demo")
alpInt = turnFromAlphabet(hrhe, 0)
print(alpInt)
#get factors
oneDimensionFactors = []
for p in range(2,alpInt):
# if mod 0
if(alpInt % p) is 0:
oneDimensionFactors.append(p)
else:
oneDimensionFactors.append(1)
indexP = 0
for z in oneDimensionFactors:
allFactors.setdefault("index{0}".format(index), {})["keyNumber"+str(p)] = z
index+=1
print(allFactors)
【问题讨论】:
-
行为与什么不同,究竟是什么?什么是预期的输出?实际输出/错误是什么?
-
期望的输出是什么?实际输出是什么?你看到了什么错误?
-
在
for else语句中,在 turnFromAlphabet() 函数中,它返回“错误:输入不在字母表中”我已将其定义为打印该错误,但我不知道为什么会这样执行,即使传递给它的参数在字母表中(第 83 行,alpInt = turnFromAlphabet(hrhe, 0)) -
您可能需要阅读How to Ask 和minimal reproducible example 并相应地编辑您的问题。另请注意,您的代码充满了明显的错误和无用的复杂事物。
标签: python python-3.x random output