2017-10-13 51 views
-1
#Passwordgen 
#Generate a password 

def main(): 

    #Ask the user to input a long string of words, separated by spaces 
    sent = input("Enter a sentance separated by spaces: ") 

    #Ask the user to input a position within each word 
    #Take the position of that word 
    pos = eval(input("Input a position within each word: ")) 
    words = sent.split(" ") 

    wordy = ""         
    #loops through the word to take the position of the letter 
    for word in words: 
     wordy = wordy + word[pos] 

    #Prints out the letters as a password 
    print("Your password is: ", wordy) 


main() 

我的教授希望我輸出從零開始,從零開始到包括用戶輸入的位置在內的每個位置生成的密碼。它應該使用密碼(短語,位置)函數來生成密碼。使用python函數調整代碼

使用字符串格式打印每個密碼輸出行,如下所示。

例如:

Enter words, separated by spaces: correct horse battery staple 
Up to position within each word: 3 

Password 0: chbs 
Password 1: ooat 
Password 2: rrta 
Password 3: rstp 
+3

你的問題是什麼?我只看到一個項目描述。另外,你爲什麼使用'eval'? – Carcigenicate

+0

這太寬了。你已經嘗試了什麼?你需要什麼特別的幫助?回答目前狀態下的這個問題只是爲你做功課。如果您有關於您已經嘗試過的具體問題,我們可以爲您提供幫助。 – Carcigenicate

回答

0

上的代碼幹得好,到目前爲止,只需要一些調整:

#Passwordgen 
#Generate a password 

def main(): 

    #Ask the user to input a long string of words, separated by spaces 
    sent = input("Enter a sentance separated by spaces: ") 

    #Ask the user to input a position within each word 
    #Take the position of that word 
    pos = int(input("Input a position within each word: ")) 
    words = sent.split(" ") 

    # set a counter variable to count each password generateed 
    count = 0 

    #loops through the word to take the position of the letter 
    for p in range(pos+1): 
     # reset wordy for each new password we are generating 
     wordy = "" 
     for word in words: 
      wordy = wordy + word[p] 
     #Prints out the letters as a password 
     print("Your password {c} is: {pw}".format(c = count, pw = password)) 
     count += 1 

main() 

我們需要跟蹤的,我們是從拍攝中的字母位置每個字,這就是for p in range(pos+1)行(我們做pos+1獲得位置高達3+1(或4),因爲範圍上升,但不包括該值。

另外,根據說明,我們需要"Use string formatting to print each of the password output lines",所以參考這些python3 format examples,我們可以格式化每個密碼的輸出和相關的計數。

希望這可以幫助你,歡呼!

+0

我是否應該總是縮寫我的變量以使字符串格式變得更容易,或者您是否有這種特殊原因? print(「你的密碼{c}是:{pw}」。格式(c = count,pw =密碼)) count + = 1 –

+0

不,沒有理由我簡化變量,對你來說似乎是合理的:)我想我只是將它們命名爲縮寫,因爲我不想用長變量名填充代碼,這使得它很難閱讀。對不起,如果它很混亂。如果可以的話,絕對使用更好的變量名稱。 format方法可以將變量關聯到順序參數,但是您可以將變量命名爲對您更有意義的任何內容。 – davedwards