2017-10-20 52 views
-3

這就是它,問題在於它不會改變名詞和形容詞的值,並且打印0表示名詞,0表示形容詞,10表示打印數字的隨機數。任何人都可以修復我的ptyhon唯一密碼生成器?

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)) 

我一直在試圖解決它,但我不明白爲什麼它不會再次設置列表。

+0

嘗試把你的代碼分解成更小的單元,並確保在嘗試將更大的東西放在一起之前正確地工作。如果您有疑問,請閱讀文檔,並使用Python調試器「pdb」逐行檢查您的代碼,並嘗試仔細考慮邏輯錯誤的位置。否則,除非有一個可重現的問題,否則很難爲您提供幫助。 – Iguananaut

+1

可能沒有多大幫助,但所有這些都可以重構爲一個循環,以便所有的代碼重複被刪除。當你發現自己一遍又一遍地輸入類似的代碼時,只有幾個不同之處,而且差別幾乎是數據,這是一個紅旗,你有一個想要寫入的函數。一旦代碼被減少,這將更容易調試。 – RobertB

回答

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)) 
1

原因是因爲你用10個零初始化名詞和形容詞列表,所以函數選擇(名詞)有90%的選擇零的機會。

代替初始化列表,然後填充它,不要聲明它,只是你的追加名詞和形容詞使用,例如:

curNoun = input("...") 
noun.append(curNoun) 
相關問題