2016-07-27 2 views
-3

이게 정말 이상한 오류가있어서 그게 뭔지 전혀 모르겠습니다.ValueError with randint

Traceback (most recent call last): 
    File "C:\Users\Andrei\Documents\USB Backup\Python\Hangman.py", line 31, in wordChoice 
      word = theme[randint(0, len(theme) - 1)] 
      File "C:\Users\Andrei\AppData\Local\Programs\Python\Python35-32\lib\random.py", line 218, in randint 
      return self.randrange(a, b+1) 
      File "C:\Users\Andrei\AppData\Local\Programs\Python\Python35-32\lib\random.py", line 196, in randrange 
      raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) 
     ValueError: empty range for randrange() (0,0, 0) 

나는 모든 곳에서 검색 한하지만 난 아무것도 찾을 수 없습니다

def wordChoice(): 
    theme = themechoice 
    word = theme[randint(0, len(theme) - 1)] 

이 오류는 다음과 같습니다 이 내 코드입니다. 나도 뭔가 신빙성이 있다면 미안해.

편집 :

내가 이것에 테마 설정하기 전에 : theme가 비어있을 때이 오류가 발생합니다

def themeChoice(): 
    n = 0 
    for i in themes: 
     n += 1 
     print(str(n) + " - " + i) 
    themechoice = themesToThemes[themes[int(input("Type the number corresponding to your chosen theme: ")) - 1]] 
    print (themechoice) 
+0

당신은'randint (0, 0)'을 요구하고 있습니다. 'len (theme)'이 1 인 것으로 보이므로, [0; 0 [ – BusyAnt

+0

이것은'randint (0, -1)'에서 얻은 것으로'randrange (0,0)'로 변환됩니다. 해결책은이 범위에서 임의의 숫자를 얻는 것이 아니라 의미가 없기 때문입니다. – khelwood

답변

1

합니다.

>>> theme = [] 
>>> random.randint(0, len(theme)-1) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Programming\Python 3.5\lib\random.py", line 218, in randint 
    return self.randrange(a, b+1) 
    File "C:\Programming\Python 3.5\lib\random.py", line 196, in randrange 
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) 
ValueError: empty range for randrange() (0,0, 0) 

theme 확인이 실제로 무작위로 그것에서 요소를 선택하기 전에 그 요소이 있는지 확인합니다. 또한 수동으로 randint으로 색인을 생성하는 대신 random.choice을 사용하는 것이 좋습니다.