有人能修复我的Python唯一密码生成器吗?

2024-06-07 13:09:50 发布

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

就是这样,问题是它不会改变名词和形容词的值,打印0代表名词,0代表形容词,10到99之间的随机数代表数字。你知道吗

from random import *
print("I get you password.")
i = 0
noun = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
adjective = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

addNoun = input("Would you like to add a noun? Type yes or no.")
if addNoun == "yes":
  while addNoun == "yes" and i < 10:
    noun[i] = input("Type a third person noun(You're first word). Use no spaces and try to use a mix of lower case and capital letters. ")
    i = i + 1
    addNoun = input("Would you like to add another noun? Type yes or no.")
elif addNoun == "no":
  noun = ["He", "Mr.BossDude", "Dad", "Mom", "Acquaintance"]
else:
  addNoun = input("I said type yes or no.")
  if addNoun == "yes":
    while addNoun == "yes" and i < 10:
      noun[i] = input("Type a third person noun(You're first word). Use no spaces and try to use a mix of lower case and capital letters. ")
      i+=1
      addNoun = input("Would you like to add another noun? Type yes or no.")
  else:
    noun = ["He", "Mr.BossDude", "Dad", "Mom", "Acquaintance"]

addAdjective = input("Would you like to add an adjective or verb? Type yes or no.")
i = 0
if addAdjective == "yes":
  while addAdjective == "yes" and i < 10:
     adjective[i] = input("Type a verb or adjective(You're second word). Use no spaces and try to use a mix of lower case and capital letters. ")
     i+=1
     addAdjective = input("Would you like to add another noun? Type yes or no.")
elif addAdjective == "no":
  adjective = ["IsGud", "Walks", "Sleeps", "Continues", "Falls", "SeesAll"]
else:
  addAdjective = input("I said type yes or no.")
  if addAdjective == "yes":
    while addAdjective == "yes" and i < 10:
      adjective[i] = input("Type a verb or adjective(You're second word). Use no spaces and try to use a mix of lower case and capital letters. ")
      i+=1
      addAdjective = input("Would you like to add another noun? Type yes or no.")
  else:
    adjective = ["IsGud", "Walks", "Sleeps", "Continues", "Falls", "SeesAll"]

number = randint(10, 99)

print("Your password is: " + str(choice(noun)) + str(choice(adjective)) + str(number))

我一直在试图解决它,但我不明白为什么它不会再次设置列表。你知道吗


Tags: orandtonoyouaddinputtype
2条回答

原因是因为用10个零初始化名词和形容词列表,所以函数选择(名词)有90%的几率选择零。你知道吗

不要先初始化列表,然后再填充列表,不要声明列表,只需使用以下命令附加名词和形容词,例如:

curNoun = input( "..." )
noun.append( curNoun )

除了数组处理不当之外,您还复制了数据和逻辑。您不需要更多的代码,只需要更少的简化:

from random import randint, choice

print("I'll provide you a password.")

nouns = []

while not nouns:
    addNoun = input("Would you like to add a noun? Type yes or no: ").lower()

    if addNoun.startswith("y"):
        while addNoun.startswith("y"):
            nouns.append(input("Type a third person noun (you're first word). Use no spaces and use a mix of lower case and capital letters.\n"))
            addNoun = input("Would you like to add another noun? Type yes or no: ").lower()
    elif addNoun.startswith("n"):
        nouns = ["He", "Mr.BossDude", "Dad", "Mom", "Acquaintance"]
    else:
        print('I said, "type yes or no."')

adjectives = []

while not adjectives:
    addAdjective = input("Would you like to add an adjective or verb? Type yes or no: ").lower()

    if addAdjective.startswith("y"):
        while addAdjective.startswith("y"):
            adjectives.append(input("Type a verb or adjective (you're second word). Use no spaces and use a mix of lower case and capital letters.\n"))
            addAdjective = input("Would you like to add another noun? Type yes or no: ").lower()
    elif addAdjective.startswith("n"):
        adjectives = ["IsGud", "Walks", "Sleeps", "Continues", "Falls", "SeesAll"]
    else:
        print('I said, "type yes or no."')

number = randint(10, 99)

print("Your password is:", choice(nouns) + choice(adjectives) + str(number))

相关问题 更多 >

    热门问题