Python 3 と PySide を使用して新しいゲームを作成しています。このゲームは、ゲーム パスワードのシンプルな UI であり、基本的にはリストからランダムな値を取得して表示し、値 (単語) が処理されたら、その単語をリストから削除して、2 回使用しないようにします。奇妙な問題は、GUI では一度に 4 つの単語しか選択できず、リストが空であることを示す ValueError をスローすることです。これは明らかに誤りであり、エラーが発生する前に 4 つの単語しか選択できないことがわかります。
#!/usr/bin/python
import sys
from PySide import QtCore, QtGui
from random import *
from game_gui import Ui_main_window
from game_list import cards
class game_window(QtGui.QWidget, Ui_main_window):
def __init__(self, parent=None):
super(game_window, self).__init__(parent)
self.setupUi(self)
global password_label
password_label = self.password_label
global get_button
get_button = self.get_button
get_button.clicked.connect(self.button_clicked)
def label_clear(self):
password_label.setText('Push Button To Get New Word')
get_button.setText('Push Me To Get A Word')
get_button.clicked.connect(self.button_clicked)
def button_rename(self):
get_button.setText('Push To Clear Word')
get_button.clicked.connect(self.label_clear)
def button_clicked(self):
card_to_play = choice(cards)
password_label.setText(card_to_play)
cards.remove(card_to_play)
self.button_rename()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = game_window()
window.show()
sys.exit(app.exec_())
エラーは次のとおりです。
Traceback (most recent call last):
File "/usr/lib/python3.3/random.py", line 249, in choice
i = self._randbelow(len(seq))
File "/usr/lib/python3.3/random.py", line 225, 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 "game_code.py", line 58, in button_clicked
card_to_play = choice(cards)
File "/usr/lib/python3.3/random.py", line 251, in choice
raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence
そして、これらのエラーは両方とも最大 50 回表示されます。
どんな助けでも大歓迎です。
編集:リクエストに応じて、ここにリストがあります。
cards = [
'scripture',
'miracle',
'garment',
'prophecy',
'tomb',
'staff',
'spirit',
'garden of eden',
'heaven',
'sulpher',
'nephelim',
'knowledge',
'armageddon',
'plague',
'commandment',
'sovereignty',
'resurrection',
'wine',
'cherub',
'sandals',
'wilderness',
'gehena',
'famine',
'temple',
'passover',
'baptism',
'leprosy',
'ark',
'drachma',
'pharaoh',
'levites',
'scroll',
'chaff',
'boils',
'Holy Spirit',
'dragon',
'lots',
'Babylon',
'tent',
'parable',
'scales',
'Urim & Thummim',
'scarlet',
'transfiguration',
'flame',
'wild beast',
'straw',
'Red Sea',
'pearl',
'emerald',
'swine',
'demon',
'Tartarus',
'wine',
'turtledove',
'gnat',
'camel',
'garment',
'shroud',
'tomb',
'Most Holy',
'curtain,'
'olive branch',
'dust',
'Cherub',
'bull',
'scorpion',
'Nephilim',
'privy',
'sacrifice',
'earthquake',
'abyss',
'fasting',
'stake',
'sling',
'Samson',
'Goliath',
'betrayer',
'slanderer',
'murderer',
'circumcision',
'astrologer',
'Hades',
'chariot',
'cistern',
'balsalm',
'undergarment',
'bruise',
'shipwreck',
'fish',
'intestines',
'conscience',
'curtain',
'hypocrisy',
'whitewash',
'grave',
'spear',
'breastplate',
'helmet',
'leviathan',
]