2014-10-17 2 views
0

Python 3.3.3에 코드를 작성하여 말을 12 개 입력하면 32 개 팀 목록을 만들고 가장 반복되는 팀이 최소한 반복되고있다. 이 작업을 수행 한 경우 :Python - "random"오류

import random 
    teams =[] 
    randoms = [] 
    team = 0 
    amount = 1 
    while team != "done": 
     team = input("Please enter team name " + str(amount) +" or enter 'done' if you have finished.\n") 
     if team != "done": 
      teams.append(team) 
      randoms.append(team) 
      amount = amount + 1 
    length = len(teams) 
    times =0 
    while len(teams) != 32: 
     while len(teams) <= 32-length: 
      for x in range (0,length+1): 
       teamname = teams[x] 
       teams.append(teamname) 
     else: 
      choice = random.choice(randoms) 
      teams.append(choice) 
      randoms.remove(choice) 
     teams.sort() 
    for x in range(0,len(teams)): 
     print (teams[x]) 

나는 프로그램을 실행하고 12 개 팀을 입력 한 다음 완료했습니다. 그것은 다음과 같은 메시지와 함께 제공 :

이 평균을 수행하고 나는이 문제를 어떻게 해결합니까 무엇
Traceback (most recent call last): 
    File "C:\Python33\lib\random.py", line 248, in choice 
    i = self._randbelow(len(seq)) 
    File "C:\Python33\lib\random.py", line 224, in _randbelow 
    r = getrandbits(k)   # 0 <= r < 2**k 
ValueError: number of bits must be greater than zero 


During handling of the above exception, another exception occurred: 


Traceback (most recent call last): 
    File "[File location]", line 30, in <module> 
    choice = random.choice(randoms) 
    File "C:\Python33\lib\random.", line 250, in choice 
    raise IndexError('Cannot choose from an empty sequence') 
IndexError: Cannot choose from an empty sequence 

?

+1

random.choice에 전달할 때 randoms가 빈 목록으로 간주됩니다. – Noelkd

+1

코드 및 역상 들여 쓰기를 정렬 할 수 있습니까? 감사합니다. – EdChum

+0

어떤 버전의 Python을 사용하고 있습니까? –

답변

0

random.choice() 함수의 배열이 비어 있으면 오류가 발생합니다. 파이썬 디버거를 사용하여 배열이 완전히 비어있을 때 알아내는 것이 좋습니다. len (팀)마다 루프를 인쇄하여 코드가 엉망이되는 부분을 표시하는 코드 행을 추가하는 데 도움이 될 수 있습니다. > 대> =와 같이 간단 할 수 있습니다.

희망이 도움이됩니다.