1
は、ここに私のコードです:のPython 3.5.3 KeyError例外:0(random.choiceから返される)
fList = {7:7, 9:9}
def checkList():
if not None in fList:
print(fList)
fL = random.choice(fList)
ttt[fL] = computerLetter
del fList[fL]
print(fL)
print(ttt)
print(fList)
Pythonは私にこのエラーがスローされます。移動キーがあったとき
{9: 9, 7: 7}
Traceback (most recent call last):
File "/home/jason/Desktop/Programming/Python3_5/TestCode.py", line 35, in <module>
checkList()
File "/home/jason/Desktop/Programming/Python3_5/TestCode.py", line 22, in checkList
fL = random.choice(fList)
File "/usr/lib/python3.5/random.py", line 265, in choice
return seq[i]
KeyError: 0
これは素晴らしい仕事をしていました:辞書の値ペア。何が間違っているのか分かりません。あなたの時間と注意のために事前にありがとうございます。
ありがとうございます。辞書が空でない限り、if文を実行したい。あなたの意見では、これを行う最善の方法は何でしょうか? – Webmatt96
Pythonの方法は、辞書の中に値があるときには 'True'に評価され、空であれば' False'に評価される 'if fList'を書くことです。あなたは辞書を使っているので、なぜそれをf * List *と呼んでいますか? – EarthDragon
本当の理由はありません。私はちょうど名前を決めた。 – Webmatt96