2016-11-16 24 views
1

我的代碼是關於用每個單詞/數字的第一個字符/數字作爲一個句子/短語中的一個字符來創建一個密碼,並且就像這樣打印它。Python:把第一個字符沒有列表

示例:停下來嗅到350朵「玫瑰花」。 - > Sast3r。 (忽略使用用r 報價)

這將是非常容易使用列表,但你不能在這個分配使用它們我的代碼。所以,我不知道要什麼有什麼我迄今所做的後現在要做的

功能:

def create_password(phrase): 
q = "'" # quotations 
dq = '"' # double quotes 

password = phrase[0] 

for i in phrase: 
    x = phrase.find(" ") 
    if i.isalnum: 
     password += phrase[x + 1] 
    elif x == q or x == dq: 
     password += phrase[x + 2] 

return password 

主營:

# Imports 
from credentials import create_password 

# Inputs 
phrase = str(input("Enter a sentence or phrase: ")) 

# Outputs 
password = create_password(phrase) 
print(password) 
+0

字符串與字符列表幾乎相同。幾乎所有可以用列表完成的事情都可以用字符串來完成。 –

+0

我認爲你到目前爲止在正確的軌道上。當我第一次看到你的問題時,我立即想到了'.find',這似乎就是你以後的想法。然而,'.find()'有第二個參數,它告訴你在什麼點開始尋找之後。 – Zizouz212

+0

你總是可以使用迭代器:'l ='abc''然後'next(iter(l))'會給你'a'; – Marcin

回答

2

我認爲遍歷整個短語更簡單,不用擔心在空間上分裂。請記住你是否剛剛看過一個空間。你只想在看到空格後添加角色。

def create_password(phrase): 
    q = "'" # quotations 
    dq = '"' # double quotes 

    #Initialize the password to be an empty string 
    password = "" 

    #We are at the start of a new word (want to add first index to password) 
    new_word = True 

    #Walk through every character in the phrase 
    for char in phrase: 

     #We only want to add char to password if the following is all true: 
     #(1) It's a letter or number 
     #(2) It's at the start of a new word 
     #(3) It's not a single quote 
     #(4) It's not a double quote 
     if char.isalnum and new_word: 
      if char != q and char != dq: 
       password += char 
       new_word = False #<-- After adding char, we are not at a new word 

     #If we see a space then we are going to be at a new word 
     elif char == " ": 
      new_word = True 

    return password 

p = create_password('Stop and smell the 350 "roses"') 
print(p) 

輸出:

Sast3r 
+0

我看到你離開.find函數來自哪裏。你的方式很好地利用布爾值來忽略與字符串混淆。偉大的工作:D – IncognitoBatman

-2

嘗試採取的第一個字符字符串中,和然後是每個跟隨空間的角色。看起來你有正確的想法。

1

你絕對走在正確的軌道上!使用str.find()方法絕對是最好的選擇!

但是,您需要了解str.find()方法的作用。看看簽名:

str.find(sub [,start [,end) -> int 

    # sub -> character to find 
    # start -> where the function should start looking in the string 
    # end -> where the function should stop looking 

    # Returns a number, which is the place it found the character. 
    # If it didn't find anything, then return -1. 

不告訴函數從哪裏開始,它總是會在字符串中找到第一個出現的字符。它不會知道你正在瀏覽字符串的每個字符。

因此,讓我們改變了一點點:

for char_index in xrange(len(phrase)): 
    # Tell the method to look after char_index: we've already looked before this! 
    x = phrase.find(' ', char_index) index 

    if phrase[x+1].isalnum(): # It's a function, notice the brackets? 
     password += phrase[x + 1] 
    elif phrase[x+2] == q or phrase[x+2] == dq: 
     password += phrase[x + 2] 

但願,這應該得到您想要的密碼。

+0

啊,謝謝你展示.find實際上做了什麼。我基本上只是在知道它計入空間數量的情況下插入它。哈哈感謝 – IncognitoBatman

1

給予優先使用內置的功能,例如,每次找到的空間位置,那麼爲什麼不直接按照空間瀉函數,讓字符串直接到字符列表中,每個元素都是一個字,然後刪除列表中的每個元素。

def create_password(phrase): 
    password = '' 
    phrase_list = phrase.split(' ') 
    print (phrase_list) 

    for i in phrase_list: 
     print (i[0]) 
     password += i[0] 
    return password 

if __name__ == '__main__': 
    # Inputs 
    phrase = str(input("Enter a sentence or phrase: ")) 

    # Outputs 
    password = create_password(phrase) 
    print(password) 
相關問題