Markov Markov - 1 month ago 9
Python Question

Python script does not work but gives no error message

I am new to programming/scripting in general and just becoming to understand the basics of the workings of python2.7. Anyways, i have here a script i have been working on to randomly generate a password made up of letters, numbers and special characters.

import random

charset="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charset+=charset.lower() # adds lowercase a-z to charset
charset+="@&%_$#" # adds these special characters to the charset
charset+="1234567890" # adds digits 0-9 to charset

def generate_password(length):
"""generates random password"""
password=[]
for n in range(length):
password.append(random.choice(charset))
print password
return password

def main():
# test cases
length = 4

# boilerplate
if __name__ == '__main__':
main()


Please excuse if this seems like a stupid question but i am new to this after all :(. When i run the above script i receive no output as well as no error message resulting in me not knowing what the heck im doing wrong!

Answer
import random

charset="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charset+=charset.lower()   # adds lowercase a-z to charset
charset+="@&%_$#" # adds these special characters to the charset
charset+="1234567890" # adds digits 0-9 to charset

def generate_password(length):

    password=[]
    for n in range(length):
        password.append(random.choice(charset))

    print password
    return password

def main():
    length = 4
    generate_password(length)


if __name__ == '__main__':
    main()

first you did not have any indentation (that might have been a copy and paste issue). Then you never called generate_password so it would have never did anything but set length to 4.