2014-09-24 4 views
-1

특정 단어를 입력 할 수있는 행맨 게임을 만들려고합니다.리스트 인덱스가 범위를 벗어남 - 파이썬

단어의 동일한 문자 수를 계산하는 코드를 만들 때 문제가 발생했습니다. 예를 들어, 'taste'에는 두 개의 t이 있습니다.

이 프로그램은 수학 합계에 사용될 문자를 계산하므로 문자는 두 번 입력 할 수 있지만 더 이상 입력 할 수는 없습니다.

나는 아래의 코드에서 범위를 벗어난 목록 색인을 발견했다.

def nolettercheck(): 

    global list45 #list of variable within code (so they can be accessed) 
    global list1 
    global countonce 
    global count0 
    global count1 
    global count2 
    global count3 
    global count4 
    global count5 
    global count6 
    global count7 
    global count8 
    global count9 
    if len(list1) == 1: #makes sure list is in range 
     if list1.count(list1[0]) > 1: """count how many letter in word and if above 1 it continues 
             (although it doesn't work perfectly yet BTW do not spoil 
              this answer for me unless 
             you have to).""" 
      count0 = list1.count(list1[0])#create variable for number of same letters 
      list45.insert(0,list1[0])#insert the letter into another list for comparison 
    if len(list1) == 2 or len(list1) < 2: """repeats with next letter until 
              similar statement =false""" 
     if list1.count(list1[1]) > 1: 
      count1 = list1.count(list1[1]) 
      list45.insert(1,list1[1]) 
    if len(list1) == 3 or len(list1) < 3: 
     if list1.count(list1[2]) > 1: 
      count2 = list1.count(list1[2]) 
      list45.insert(2,list1[2]) 
    if len(list1) == 4 or len(list1) < 4: 
     if list1.count(list1[3]) > 1: 
      count3 = list1.count(list1[3]) 
      list45.insert(3,list1[3]) 
    if len(list1) == 5 or len(list1) < 5: 
     if list1.count(list1[4]) > 1: 
      count4 = list1.count(list1[4]) 
      list45.insert(4,list1[4]) 
    if len(list1) == 6 or len(list1) < 6: 
     if list1.count(list1[5]) > 1: 
      count5 = list1.count(list1[5]) 
      list45.insert(5,list1[5]) 
    if len(list1) == 7 or len(list1) < 7: 
     if list1.count(list1[6]) > 1: 
      count6 = list1.count(list1[6]) 
      list45.insert(6,list1[6]) 

코드가 계속되어 10 자 단어까지 사용할 수 있습니다. 이 코드는 no 문자를 변수에 저장한다고 가정합니다.이 변수는 나중에 변경 될 수도 있지만 나중에 해당 행의 아래쪽 목록으로 바뀝니다. 또한 편지를 보관해야합니다 (둘 이상의 문자가있는 경우).

오류는 특히 if list1.count(list[6]) > 1:에 유사한 행에 초점을 맞추고 있습니다.

이전에이 오류가 발생하여 if len(list1) == 7 or len(list1) <7:이 작동했습니다 (목록의 허수 값 검사가 중지됨). 그래서 나는 이것에 대해 꽤 혼란 스럽다.

+2

아프다. 이것은 고통 스럽다. 10 개의 개별 변수 대신 10 개의 계수를 만드는 것을 고려해야합니다. 동일한 코드를 7 번 복사하고 붙여 넣는 대신 '범위 (1, 8)'을 반복하고, 목록보다 더 적절한 데이터 구조를 사용하는 것이 가장 좋습니다 당신은'count'와'insert'를 계속 호출해야합니다 ... – abarnert

+0

또한'len (list1) == 7 또는 len (list1) <7'은'len (list1) <= 7'으로 쓸 수 있습니다. – abarnert

+0

당신이 이걸로 뭘하려고하는지 좀 더 자세히 설명해 주시겠습니까? 내가 이해하는 바로는 단어의 문자 수를 원한다. (그러나 2로 제한하라.) 나는 당신이 여기있는 것이 매우 비효율적 인 것처럼 이것을하는 훨씬 더 간단한 방법이있을 것이라고 믿습니다. –

답변

0

단어의 글자 수 사전을 가져 오는 방법입니다. 이것을하기의 "pythonic"방법이 더있다 그러나 당신이 지금 막 시작하는 경우에, 이것은 당신을 도울지도 모른다.

a = 'somestring' 
letter_count_dict = {} 
for letter in set(a): 
    letter_count_dict[letter] = a.count(letter) 

단어의 구분 문자가

set(a) #set takes the distinct values. If you want a list of distinct values, an easy way to do it is list(set(a)) but since sets are iterable you don't usually need to do this. 

와 편지, L 주어, 그 발생 횟수는

letter_count_dict[l] #if l='s', then this would output 2 

당신이 수를 일단, 당신이 할 수있는 인 그들과 함께 할 일을 선택하십시오 ... 루프를 찾아서 하나 이상의 사건 등을 발견하십시오. 행운을 빈다.

+0

감사합니다. 나는 각 편지에 대해 전체 사전을 만드는 것을 피하기를 바랬지 만, 지금 내가 가지고있는 것보다 약간 더 효율적 일 것입니다. –

관련 문제