-3
我有這個奇怪的錯誤,我不知道它是什麼。 這是我的代碼:ValueError with randint
def wordChoice():
theme = themechoice
word = theme[randint(0, len(theme) - 1)]
這是錯誤:
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 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)
你在問'randint(0,0)'。看起來'len(主題)'是1,因此在[0; 0 [ – BusyAnt
這就是你從'randint(0,-1)'得到的結果,它被轉換爲'randrange(0,0)'。解決方法不是嘗試獲得該範圍內的隨機數,因爲它沒有任何意義。 – khelwood