Reputation: 53
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!
Upvotes: 2
Views: 16128
Reputation: 714
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 done anything but set length to 4.
Upvotes: 3
Reputation: 11410
It's because, your main() function doesn't do anything. Change it as following :
def main():
# test cases
length = 4
#
print generate_password(length)
# boilerplate
if __name__ == '__main__':
main()
Upvotes: 8