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

时间:2017-10-20 16:57:43

标签: python python-3.x

就是这样,问题是它不会改变名词和形容词的值,并且为名词打印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))

我一直在努力修复它,但我不明白为什么它不会再次设置列表。

2 个答案:

答案 0 :(得分:1)

原因是因为你用十个零来初始化你的名词和形容词列表,所以函数选择(名词)有90%的机会选择零。

不要初始化列表然后填写它,不要声明它,只是使用例如附加你的名词和形容词:

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

答案 1 :(得分:0)

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

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))
相关问题