简体   繁体   English

Python脚本不起作用,但未显示错误消息

[英]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. 我一般对编程/脚本是陌生的,只是逐渐了解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! 请原谅,如果这似乎是一个愚蠢的问题,但我毕竟是新手:( ..当我运行上述脚本时,我没有收到任何输出,也没有收到错误消息,导致我不知道该怎么做!

It's because, your main() function doesn't do anything. 这是因为,您的main()函数没有执行任何操作。 Change it as following : 进行如下更改:

def main():
  # test cases
  length = 4
  #
  print generate_password(length)

  # boilerplate 
if __name__ == '__main__':
  main()
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. 然后,您永远不会调用generate_password,因此它将长度设置为4不会做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM