I'm trying to to create a password generator I'm trying to create the code bit in the form of functions but when I put the file reader part in a function its saying f is not defined
enter code here
num=int(input("enter how many characters you want in the password="))
def file_reader(f):
#this is to creat a txt file and store the characters in it
f=open("guru99.txt","w+")
f=f.write("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
f=open("guru99.txt","r")
f=f.read()
f=list(f)
return f
def password_generator(num,f):
#this is to pick random amount of characters from the list
import random
password=""
for i in range(num):
password+=random.choice(f)
return password
#this is to run the functions
file_reader(f)
test=password_generator(num,f)
print(test)
I don't understand why you are writing and reading your characters to a file. You can simply choose a random character from a string like this:
num=int(input("enter how many characters you want in the password="))
f = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def password_generator(num,f):
#this is to pick random amount of characters from the list
import random
password=""
for i in range(num):
password+=random.choice(f)
return password
#this is to run the functions
test=password_generator(num,f)
print(test)
This works in Python 3 (and 2).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.