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
?
random.choice에 전달할 때 randoms가 빈 목록으로 간주됩니다. – Noelkd
코드 및 역상 들여 쓰기를 정렬 할 수 있습니까? 감사합니다. – EdChum
어떤 버전의 Python을 사용하고 있습니까? –