mercredi 17 février 2021

Is there any way I can concatinate string, numbers and symbols, using the open() write function on multiple lines?

Is there any way I can concatinate string, numbers and symbols, using the open() write function, to 1) Multiple lines, and 2) max lenght of chars, say 6-maxlenght. I have two questions regarding a Python program I've written, that in theory should generate a list of somewhat strong passwords, automating a usually tedious process, I know Mozilla does this automatically, but this program is just something I've written for myself to learn the language and CS in general.

So, here is the whole code, stripped of a few personal comments, also, is the return generatePassword() redundant in the if/else statement on line 23 and 26?

""" begins here """

import random
import time


randChar = ["A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] # Silly question me and my friends are asking ourselves regarding the random.choice() module as the professor uses from module import <function name> rather than module.<function name>, is there any difference in compiling speed and resource use? 

randNum = ["1", "2", "3", "4", "5", "6", "7",
           "8", "9", "10", "15", "20", "25", "30", "35", "40", "45",
           "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100",
           "150", "200", "250", "300", "350", "400", "450", "500", "550", "600", "650", "700", "750", "800", "850", "900"
           "950", "1000", "1250", "1300", "1450", "1500", "2000", "3000"] # is random.randint() a better option? 
 
randSymbol = ["@", "$",
                   "!", "+", "§"]
def generateFile():
            file_object_gen_yn = str(input(r"Do you wish to generate a new password.txt in your current working directory? y/n ")).lower()
            if file_object_gen_yn == "y":
                        print(r"created a new file: C/:")
                        file_Object = open("passwords.txt", "x")
                        return generatePassword()
            else:
                        print("Assuming file already exists in current working directory, continuing running function genererateUser")
                        print("If the file does not exists, a syntax error is likely to occur.")
                        return generatePassword() # alright, here I assume the file password.txt already exists, which means(?) that in the function generatePassword() this should still behave the same way, BUT, not generate the file passwords.txt as it already exists? If neither of this is true, see print statement on line 26.
            



def generatePassword():
    print("The program is starting, it will create circa n passwords where n = range(0,n) in for loop, to stop the program hold ctrl+shift+C \n")
    print("Starting...\n")
    time.sleep(1)

    
    for i in range(0, 3): # set this interval before running to have a set number of accounts created.
        print(random.choice(randChar)  + random.choice(randNum) + random.choice(randSymbol))
        file_Object = open(r"passwords.txt", "a") # unless this file exists the program will prompt an error message, possible flags to run open(a),open(x),open(r),open(w)
        file_Object.write(random.choice(randChar) + random.choice(randNum) + random.choice(randSymbol), "%6")   
generateFile()
#generatePassword() # I think I can comment this function out since I am using generateFile() to return generatePassword

So to summerise, I have tested and gotten the following results:

The program creates passwords.txt if I input y into my first input() prompt. I assume I will have the same result if the file already exists in the current working directory,but I have not yet tested that. The program creates the file, it generates one passwords in the .txt file, and in this code, 3 strings in the console(print statement on line 38).




Aucun commentaire:

Enregistrer un commentaire