2017-10-13 49 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