2014-12-03 74 views
-2

我目前正在爲我的Python類做一個程序。描述在這裏:字符串切片按預期工作

編寫一個簡短的python程序YI_swapOandE.py,用'e'替換'o'。提示用戶輸入一個單詞並顯示它的交換版本(如果它包含o)。否則,程序將只顯示原始單詞。這個程序必須使用你在Hangman程序中學到的切片技術。 實例: 鍵入單詞,我會用一個「e」替換任何「O」:掃帚 調換的話就是:breem

到目前爲止,之後她告訴我們重新使用一些代碼,我這樣做:

def splicing(): 
    word = input("Please input a word") 
    for i in range(len(word)): 
     letter = word[i] 
     if letter == 'o': 
      word = letter[:i] + word[i] + letter[i+1:] 
    print(word) 


def main(): 
    splicing() 


main() 

是的我明白,有些位並不完美,但我只需要一點幫助。

目前,我得到的錯誤:字符串索引超出範圍

我該如何解決這個問題?

+0

'letter [:i] + word [i] + letter [i + 1:]'看起來像是在一封信的中間粘貼一個單詞,而不是反過來。 – 2014-12-03 21:20:48

+0

'word = word [:i] +'e'+ word [i + 1:]' – 2014-12-03 21:22:34

+2

您將獲得今日最少發言題的標題;-) – Alfe 2014-12-03 21:31:33

回答

0

replace可以做你想要達到的目標:

>>> "hello how are you".replace('o','e') 
'helle hew are yeu' 
>>> "broom".replace('o','e') 
'breem' 

一些修改代碼:

def splicing():  
    word = input("Please input a word") 
    modify = '' 
    for i in range(len(word)): 
     if word[i] == 'o': 
      modify += 'e' 
     else: modify += word[i] 
    print(modify) 

def main(): 
    splicing() 

main() 
+1

OP沒有在字符串上循環。因爲我在範圍內(len(word))循環遍歷整數列表。 – munk 2014-12-03 21:20:33

+0

糾正我,我錯了嗎? – Hackaholic 2014-12-03 21:25:30

+0

你正在討論修改你正在循環的東西,而這只是在這段代碼中沒有發生。 – munk 2014-12-03 21:27:57

0

您定義letter是單個字符:

letter = word[i] 

但然後在這一行:

word = letter[:i] + word[i] + letter[i+1:] 

......你正在切片。這是錯誤的原因。將該行中的letter這兩個引用更改爲word可能是您正在嘗試執行的操作,但是您還需要將其中已有的一個引用更改爲word。 (提示:因爲當你看到一個'o'時你只碰到這個區域的內部,所以你確切地知道你想要去哪裏,不需要變量。)

3

問題出在letter = word[i]。我覺得你很困惑它是什麼。

你會像對待它是一個字符串與多個字符。

def splicing(): 
    word = input("Please input a word") 
    for i in range(len(word)): 
     letter = word[i] 
     if letter == 'o': 
      word = word[:i] + 'e' + word[i+1:] 
    print(word) 


def main(): 
    splicing() 


main()  
+0

謝謝!這工作 – 2014-12-03 21:26:41

+0

嗨@StephendeRiel如果這個或任何答案已解決您的問題,請考慮[接受它](http://meta.stackexchange.com/q/5234/179419)通過點擊複選標記。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – munk 2014-12-03 21:29:52

1

您的代碼

def splicing(): 
    word = input("Please input a word") 
    for i in range(len(word)): 
     letter = word[i] 
     if letter == 'o': 
      word = letter[:i] + word[i] + letter[i+1:] 
    print(word) 

讓我們來看看

 letter = word[i] 
     if letter == 'o': 

可以簡化爲

 if word[i] == 'o': 

現在,讓我們來看看你做了什麼,當你發現一個 'O'

使用
  word = letter[:i] + word[i] + letter[i+1:] 

你以前使用的letter你有

  word = word[i] + word[i] + word[i] 

,這不是你想要的,首先是因爲你不把一個「e」你的話......

  word = word[:i]+'e'+word[1+1:] 

總結起來

def splicing(): 
    word = input("Please input a word") 
    for i in range(len(word)): 
     if word[i] == 'o': 
      word = word[:i]+'e'+word[i+1:] 
    print(word) 

上述過程我在當前級別適用,但效率不高,因爲您正在構建一次又一次被稱爲word的字符串......它將字符串轉換爲可以在適當位置更改的數據結構,操作替代和轉換回一個字符串

>>> word = 'Popeye & Olive' 
>>> l = [c for c in word] 
>>> print(l) 
['P', 'o', 'p', 'e', 'y', 'e', ' ', '&', ' ', 'O', 'l', 'i', 'v', 'e'] 

現在你可以應用修改,同時建立新的數據結構:

>>> l = ['e' if c == 'o' else c for c in word] 
>>> print(l) 
['P', 'e', 'p', 'e', 'y', 'e', ' ', '&', ' ', 'O', 'l', 'i', 'v', 'e'] 

這是非常具有可讀性,不是嗎?

現在let'g更進一步,改變它的資本「O」

>>> l = ['e' if c == 'o' else 'E' if c == 'O' else c for c in word] 

這是一個有點複雜,但仍然可讀......

>>> print(l) 
['P', 'e', 'p', 'e', 'y', 'e', ' ', '&', ' ', 'E', 'l', 'i', 'v', 'e'] 

但這並不是什麼我們想要,我們想要一個字符串!

爲此,Python提供了str.join()方法---在這裏,我們想用空字符串""

>>> print("".join(l)) 
Pepeye & Elive 
>>> 

加盟l元素如果我們想要把它放在一起

>>> word = "".join(['e' if c=='o' else 'E' if c=='O' else c for c in word]) 
>>> print(word) 
Pepeye & Elive 
>>>