2015-11-06 110 views
0

我的代碼假設將一個字符串轉換爲一個函數,然後將其中每個字符的大寫字母從h切換到H和E,但是我在某種程度上遇到了錯誤 爲什麼?爲什麼我在python上得到這個錯誤,我該如何解決它? (循環和字符串)

這是錯誤messege:

chr = str[i] 

類型錯誤:字符串索引必須是整數,而不是str的

我的代碼是:

def CapsChanger(str): 

    i = str[0] 
    for i in str : 
     chr = str[i] 

     if((ord(chr) > 46) and (ord(chr) < 91)): 
      str[i].upper() 

     if((ord(chr) > 96) and (ord(chr) < 126)): 
      str[i].lower() 
    print str  

str = raw_input() 

CapsChanger(str) 

input() 
+1

'for i in str'迭代實際的字符,無指示。不需要'chr = str [i]'。 –

+0

這是什麼意思? – TotalyNotUbisoft

回答

3

當你for i in str,在每次迭代i表示實際字符,而不是索引。所以你不需要做chr = str[i] - i已經是那個角色了。

+0

順便說一句,Python沒有「字符」的概念:一切都是一個字符串;)。 – 3442

+1

關於錯誤本身的解釋也可能有幫助。在這種情況下'str [i]'相當於'str ['0']'這會混淆評估者​​。 '字符串索引必須是整數'正在抱怨接收不是整數的索引。 –

+0

好的謝謝! 這也是一個很好的事實,即時通訊新的這種語言,所以我有我的這裏落在這裏 – TotalyNotUbisoft

0

istring而不是index。如果您需要index使用enumerate

ord(chr)使用 string代表的一封信
for idx, i in str: 
    print idx, i 

insted的。

如果條件使用Chained Comparisons,則缺少兩個條件。

def CapsChanger(str): 
    out = [] 

    for idx,chr in enumerate(str): 
     if 'Z' >= chr >= 'A': 
      out.append(chr.lower()) 

     elif 'z' >= chr >= 'a': 
      out.append(chr.upper()) 
    print(''.join(out)) 
+0

我試過這樣做,但它並沒有真正改變字符串內的帽子.update()有一部分在這? – TotalyNotUbisoft

+0

@TotalyNotUbisoft'str [i] .upper()'將返回'str [i]'的大寫版本,但它本身不會改變'str [i]'。而且,Python中的字符串是不可變的,所以你必須從原始的字符串中建立一個新的字符串;您將無法在原地進行更改。 - 編輯 - 忍者。:P –

1
import string 

def invertCase(text): 
    ## Creating a list where results will be stored 
    results = list() 
    ## This will contain the abc in upper and lowercase: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 
    abc = string.lowercase + string.uppercase 

    ## Looping each letter of the received text 
    for letter in text: 
     ## If the current letter of the loop exists in our abc variable contents, it means it's a letter and not a symbol or space 
     ## So we can apply upper() or lower() to the letter. 
     if letter in abc: 
      ## If the letter is currently uppercase, then we turn it into lowercase 
      if letter.isupper(): 
       results.append(letter.lower()) 
      ## If the letter is currently lowercase, then we turn it into uppercase 
      else: 
       results.append(letter.upper()) 
     ## The current letter of the loop is not in our abc variable so it could be anything but a letter 
     ## So we just append it to our results list 
     else: 
      results.append(letter) 

    ## Once the loop finishes we just join every item in the list to make the final string and return it 
    return ''.join(results) 

print invertCase('SoMeOnE Is hAvING fUN') 

輸出:

sOmEoNe iS HaVing Fun 
+0

你能解釋一下嗎? – TotalyNotUbisoft

+0

當然!讓我添加評論並更新我的答案 –

+0

@TotalyNotUbisoft完成! –

0

變量i是已經1個字符串因爲for循環過程絃樂器這種方式。另外,當你調用str [i] .upper()時,它需要被分配給某個東西,或者打印出來,否則該字符不會實際改變。 .lower()和.upper()也有一個行爲,它已經爲你檢查了範圍,並返回相同的字符。例如。如果它已經是大寫或數字,那麼upper()將返回相同的字符。

你的函數可以如下簡化:

import sys 
def CapsChanger(str): 
    for i in str: 
     sys.stdout.write (i.lower() if (i.upper() == i) else i.upper()) 
    print 

str = raw_input() 
CapsChanger(str) 

sys.stdout.write用來避免打印出多餘的空格。三元運算符用於一次性切換案例:

<value1> if <condition> else <value2>