0

I am making a password generator in python. So what i want my code to do is take the website name from me and generate a random password and then save the website name password alongside each other in an excel sheet. the problem is that when i run the code the website gets saved but the password doesn't. Here is the code:

import random
import pyperclip

def generate_password():
    password = ''
    for x in range(0,int(n_of_char)):
        password = password + random.choice(characters)

    print(password)
    pyperclip.copy(password)

print('this is a password generating application')
characters = 'qwrtyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=!@#$%^&*()__+":;|[]'
n_of_char = input('number of characters in your password: ')
website_name = input('website name: ')

wb = xl.load_workbook('password_database.xlsx')
sheet = wb['Sheet1']

for row in range(2, sheet.max_row + 2):
    password = ''
    cell = sheet.cell(row, 1)
    if website_name == cell.value:
        ask_permission = input('the website which you have entered already has a password in the database. Do you want to replace the password?')
        current_row = row
        if ask_permission == 'y':
            generate_password()
            another_cell = sheet.cell(current_row,2)
            another_cell.value = password
            break
        else:
            print('the password was not replaced')
            break
    elif cell.value == None:
        
        generate_password()
        cell.value = website_name
        pass_cell = sheet.cell(row, 2)
        pass_cell.value = password
        break
wb.save('password_database.xlsx')
hasan ramp
  • 44
  • 8
  • Your question should be about a _specific problem_, with a [mre] that's the shortest code that does nothing but generate that problem when run. – Charles Duffy Mar 24 '21 at 12:50
  • Where you have written `pass_cell.value = password`, or `another_cell.value = password`, why do you expect `password` to be equal to something other than `''` (how it was set at the beginning of the function)? – Karl Knechtel Mar 24 '21 at 12:52
  • _nod_, this looks like the OP is expecting `generate_password` to change the `password` variable _outside_ the function. – Charles Duffy Mar 24 '21 at 12:52
  • ...which would make it a duplicate of [How to change the scope of a variable in a function](https://stackoverflow.com/questions/21169097/how-to-change-the-scope-of-a-variable-in-a-function-python). – Charles Duffy Mar 24 '21 at 12:53
  • I've seen countless variations of this problem and I feel like there should be a better duplicate to refer them to, but I guess that works. – Karl Knechtel Mar 24 '21 at 12:54

0 Answers0