2014-09-29 5 views
0
#Dancing Text :D 
listinput = raw_input("Input text(usually between 1-10 letters ") 
dance_time = raw_input("Alternate How Many Times? ") 
listinput = list(listinput) 
length = len(listinput) 
alternate = 0 
counter = 0 
first_input = listinput 
swap_counter = 0 
tempchar = "" 
counter = int(counter) #Just to make sure...getting errors 

#above are just defining variable types and changing types to lists. 

while counter <= length: 
    if alternate == 0: #first, third, fifth character 
     alternate = alternate + 1 
     counter = counter + 1 
    elif alternate == 1: #second, fourth, sixth character and so on.... 
     tempchar = listinput[counter] 
     tempchar = str(tempchar) 
     tempchar = tempchar.swapcase() 
     listinput[counter] = tempchar #It should be the number 1 the first time it runs, but it gives a index error. 
     alternate = alternate - 1 
     counter = counter + 1 

while swap_counter <= dance_time: 
    print first_input 
    print input 
    swap_counter = swap_counter + 2 

이 오류이며 색인 생성 오류를 찾았으며 '1'은 목록의 범위에 있어야합니다. (테스트 할 때마다 'hello'를 입력합니다)인덱싱 오류

올바르게 이해한다면 20 번 줄은 "listinput [1]"이어야하고, 변수 'counter'는 1 번만 실행되므로 1 번만 실행해야합니다.

답변

0

목록의 색인이 1부터 시작한다고 가정하는 것이 문제입니다. 실제로는 0부터 시작합니다. 목록에있는 항목이 하나라도 있으면 [0] 해당 항목이 제공됩니다. 카운터의 길이와 동일 할 수 있기 때문에

그래서,

tempchar = listinput[counter] 

당신이

counter < length 

는 경우가 여기에하지 않은, 사실 있는지 확인해야 실행할 수 있어야합니다 .

+0

당신을 감사합니다! 색인 생성이 0에서 시작되었음을 알고 있었지만 <= 기호를 수행하는 동안이를 고려하지 않았습니다. 많이 감사합니다 –

0

dance_time은 문자열입니다. 일이 도움이 될 수있는 경우 swap_counter <= int(dance_time):

몇 가지를 사용하십시오 :

당신은 tempchar을 필요 없어요. singlr 라인에

tempchar = listinput[counter] 
    tempchar = str(tempchar) 
    tempchar = tempchar.swapcase() 
    listinput[counter] = tempchar 

: 당신은 코드를 변환 할 수 있습니다 당신은 그냥 alternate의 값에 따라 문자를 교대의 경우를 swaping하는

listinput[counter] = str(listinput[counter]),swapcase() 

공지 사항. 나는 이것이 실제 프로그램에서이 예제에서 0으로 설정되어 있다고 생각하지만 실제로 변경하려고합니다. 그렇다면, 당신은 시도 할 수 있습니다 다음

for i, l in enumerate(listinput): 
    if alternate == 0: 
     if i%2 == 1: # odd samples 
      listinput[i] = str(listinput[i]).swapcase() 
    elif alternate == 1: 
     if i%2 == 0: # even samples 
      listinput[i] = str(listinput[i]).swapcase() 
    else: 
     pass 

또는 단순히를 :

alterante 쉽게 전에 if 상태를 확인 할 수 있습니다 0 또는 1이라고 가정하고, 같은 일을하다
for i, l in enumerate(listinput): 
    if (alternate+i)%2 == 1: # odd samples 
     listinput[i] = str(listinput[i]).swapcase() 

.

마지막으로, 파이썬의 함수는 최상위 값입니다. 그래서 당신은 또한과 같이, 함수를 반환하고 조건으로 그것을 사용할 수 있습니다

def evenOdd(alternate): 
    if alternate == 0: return lambda x: x%2 == 1 
    elif alternate == 1: return lambda x: x%2 == 0 

print [ (l.swapcase() if evenOdd(alternate)(i) else l) for i, l in enumerate(listinput) ] 

이 읽을 수있는 teenie의 비트 쉽게 ...

을 다시 여기에, 나는 가지 간단한 유지했다.0 또는 1 이외의 다른 문자가있는 경우 함수 내에서 raiseValueError 함수가 필요할 것입니다.

마지막으로, 위의 예에 따라, 당신은 또한 수행 할 수 있습니다

[(l.swapcase() if (i+alternate)%2==0 else l) for i, l in enumerate(listinput)] 
+0

고마워요. 나중에 깨달았습니다. –