lundi 20 avril 2020

Why does Python 3 for loop output and behave differently?

This is a password generator, I couldn't really determine where the problem is, but from the output, I could say it's around turnFromAlphabet()

The function turnFromAlphabet() converts an alphabetical character to its integer value.

The random module, I think doesn't do anything here as it just decides whether to convert a character in a string to uppercase or lowercase. And if a string is in either, when sent or passed to turnFromAlphabet() it is converted to lowercase first to avoid errors but there are still errors.

CODE:

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)



Aucun commentaire:

Enregistrer un commentaire