2017-10-20 3 views
-3

これは、名詞と形容詞の値を変更せず、名詞の場合は0、形容詞の場合は0、数字の場合は10と99の間の乱数を出力するという問題です。誰も私の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'を使って一度に1行ずつコードを実行し、ロジックが間違っている場所を注意深く検討してみてください。それ以外の場合、再現性のある問題が1つしかないかぎり、あなたを助けるのは難しいです。 – 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) 
関連する問題