hom高级python脚本

2024-06-26 13:47:58 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我理解这个代码吗? 我的朋友寄给我,但不想帮助我理解它,这是我的家庭作业。 如果你为每一行代码写下一个标签来帮助我理解它,我将不胜感激。你知道吗

from random import *
char="""1234567890+!#$%&/()=?qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM
,.-;:_*@<>≤≥©™£€∞§|[]≈±`•Ω醵üıπß∂ƒ¸˛√ª÷≈ç‹›‘’°˝É‡˜ÜŒ∏◊∑∆∫¯˘¬º⁄Ç«»“”"""
char=list(char)
while True:
    while True:
        passlen = int(input("""Password must between 8 and 16 character   """))
        if 8 <= passlen <= 16:
            break

    for i in range(passlen):
        print(char[randint(0, len(char))], end="")
    print()

Tags: 代码fromimporttrue朋友random标签list
1条回答
网友
1楼 · 发布于 2024-06-26 13:47:58

passlen是一个字符串,如果您试图将其转换为整数,则不应这样做,而应使用len获取输入字符串的长度

更正下面的代码,根据密码中的字符数,它从字符列表中随机选取这些字符,并打印出该字符串

from random import *

#List of characters to choose from
s="""1234567890+!#$%&/()=?qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM
,.-;:_*@<>≤≥©™£€∞§|[]≈±`•Ω醵üıπß∂ƒ¸˛√ª÷≈ç‹›‘’°˝É‡˜ÜŒ∏◊∑∆∫¯˘¬º⁄Ç«»“”"""

while True:
    while True:

        #Take the password string as input, and use len to get the length of password
        passlen = len(input("""Password must between 8 and 16 character   """))

        #Once the password of correct length is printed, exit the loop
        if 8 <= passlen <= 16:
            break

    #For the length of password, pick a random character from string s and print it
    for i in range(passlen):
        print(s[randint(0, len(s))], end="")
    print()

输出看起来像

Password must between 8 and 16 character   abcdefgh
«9¯
’r|)
Password must between 8 and 16 character   abcdefghij
d2◊∫r!lT¯ı
Password must between 8 and 16 character   abcd
Password must between 8 and 16 character   abcdefghijkl
ÉtBWmeFVZr_÷

相关问题 更多 >