是否可以使用任何语句再次运行代码?

发布于 2025-02-12 04:13:06 字数 968 浏览 1 评论 0原文

import random
import sys


print("Password Generator")    
print("* Attention: - You may not generate a password with more than 70 characters\n"
      "             - You may not enter letters")

characters = "abcdefghijklmnopqrstuvwxyz" \
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
pass_length = input("Enter your password length: ")

在这里,我只想确保用户输入是数字而不是字母的信息!

if pass_length.isnumeric() is False:
    sys.exit("Natural numbers only!")

是否有一个语句可以在激活上面的“ if”语句之后自动运行脚本?

 def password_gen_start():
        password = "".join(random.sample(characters, int(pass_length)))
        print("Your password has been generated: " + format(password))


if int(pass_length) >= 71 and pass_length.isnumeric() is True:
    sys.exit("Password cannot be longer than 70 characters!")

if int(pass_length) < 71 and pass_length.isnumeric() is True:
    password_gen_start()
import random
import sys


print("Password Generator")    
print("* Attention: - You may not generate a password with more than 70 characters\n"
      "             - You may not enter letters")

characters = "abcdefghijklmnopqrstuvwxyz" \
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
pass_length = input("Enter your password length: ")

Here I just want to make sure the information that the user's input are numbers, not letters!

if pass_length.isnumeric() is False:
    sys.exit("Natural numbers only!")

Is there a statement that can automatically run the script again after the "if" statement above is activated?

 def password_gen_start():
        password = "".join(random.sample(characters, int(pass_length)))
        print("Your password has been generated: " + format(password))


if int(pass_length) >= 71 and pass_length.isnumeric() is True:
    sys.exit("Password cannot be longer than 70 characters!")

if int(pass_length) < 71 and pass_length.isnumeric() is True:
    password_gen_start()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2025-02-19 04:13:06

您只需要在一段时间内将其放置:

stop = ""
while stop != "yes":
    pass_length = input("Enter your password length: ")
    while not pass_length.isnumeric():
        pass_length = input("Natural numbers only !\n Enter your password length: ")
    # you put here the stuff that you want to execute
    stop=input("do you want to stop ?")

You just have to put it in a while loop :

stop = ""
while stop != "yes":
    pass_length = input("Enter your password length: ")
    while not pass_length.isnumeric():
        pass_length = input("Natural numbers only !\n Enter your password length: ")
    # you put here the stuff that you want to execute
    stop=input("do you want to stop ?")
柳若烟 2025-02-19 04:13:06

谢谢Nicolas,现在我终于可以完成我的代码:

import random

print("Password Generator")
print("* Attention: - You may not generate a password with more than 70 characters\n"
          "             - You may not enter letters")
characters = "abcdefghijklmnopqrstuvwxyz" \
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
pass_length = input("Enter your password length: ")
while not pass_length.isnumeric() or int(pass_length) >= 71:
    pass_length = input("Natural numbers no larger than 70 only !\nEnter your password length: ")

def password_gen_start():
    password = "".join(random.sample(characters, int(pass_length)))
    print("Your password has been generated: " + format(password))

if int(pass_length) < 71 and pass_length.isnumeric() is True:
    password_gen_start()

Thank you Nicolas, now I can finally finish my code:

import random

print("Password Generator")
print("* Attention: - You may not generate a password with more than 70 characters\n"
          "             - You may not enter letters")
characters = "abcdefghijklmnopqrstuvwxyz" \
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
pass_length = input("Enter your password length: ")
while not pass_length.isnumeric() or int(pass_length) >= 71:
    pass_length = input("Natural numbers no larger than 70 only !\nEnter your password length: ")

def password_gen_start():
    password = "".join(random.sample(characters, int(pass_length)))
    print("Your password has been generated: " + format(password))

if int(pass_length) < 71 and pass_length.isnumeric() is True:
    password_gen_start()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文