我得到一个错误,它说我的列表是空的,当它不是

时间:2016-06-20 02:13:59

标签: python random

我正在创建一个单词jumble游戏,即时通讯问题我说我的while循环是空的,我真的可以使用一些帮助继承我的代码谢谢。

import random

count = 0

word = 'index'
wordLength = (5)

print('the word is', word ,'\n')

high = len(word)
low = -len(word)

for i in range(15):
    position = random.randrange(low, high)

listf = [0,1,2,3,4]

randomletter = random.choice(listf)


for each in range(wordLength):
    while count < 6:

        if randomletter == 0:
            listf.remove(0)
            randomletter = random.choice(listf)
            print(word[0])
        elif randomletter == 1:
            print(word[1])
            listf.remove(1)
            randomletter = random.choice(listf)
        elif randomletter == 2:
            print(word[2])
            listf.remove(2)
            randomletter = random.choice(listf)
        elif randomletter == 3:
            print(word[3])
            listf.remove(3)
            randomletter = random.choice(listf)
        elif randomletter == 4:
            print(word[4])
            listf.remove(4)
            randomletter = random.choice(listf)
        count += 1


inputs = input('press enter to exit')        

以下是我得到的错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py", line 253, in choice
    i = self._randbelow(len(seq))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py", line 230, 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 "/Volumes/sec.student/Y9/Y9B/Oliver McLean/jumbler2.0.py", line 43, in <module>
    randomletter = random.choice(listf)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py", line 255, in choice
    raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence

0 个答案:

没有答案