前提・実現したいこと
discord.pyでゲームを作ろうとしています。
機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
File "bk.py", line 32, in match if p == 0: UnboundLocalError: local variable 'p' referenced before assignment The above exception was the direct cause of the following exception:
該当のソースコード
python
1import discord 2from discord.ext import commands 3import random 4prefix = commands.Bot(command_prefix='$') 5p = p1 = p2 = bet = stay = mere = bh1 = bh2 = room = 0 6turn = True 7hand1 = [] 8hand2 = [] 9limit = 3 10sum = 13 11card = [1,2,3,4,5,6,7] 12async def TURNEND(): 13 if turn == True: 14 turn = False 15 await room.send(f'{p1.mention}turn') 16 else: 17 turn = True 18 await room.send(f'{p2.mention}turn') 19@prefix.event 20async def on_ready(): 21 print('start') 22@prefix.command(aliases=['m']) 23async def match(ctx): 24 if p == 0: 25 room = ctx 26 p1 = room.author 27 await room.send(f'{p1.mention}owner') 28 p += 1 29 elif p == 1: 30 p2 = room.author 31 await room.send(f'{p2.mention}guest') 32 p += 1 33 await room.send('start') 34 bet += 1 35 bh1 = card.pop(random.randint(0,len(card) - 1)) 36 bh2 = card.pop(random.randint(0,len(card) - 1)) 37 hand1.append(card.pop(random.randint(0,len(card) - 1))) 38 hand2.append(card.pop(random.randint(0,len(card) - 1))) 39 await room.send(f'{p1.mention}turn') 40 await p1.dm_create().send(f'?={bh1}') 41 await p1.dm_create().send(f'?={bh2}') 42 else: 43 await ctx.send('error')
#####疑問に思ってること
5行目に定義してるのに32行目に定義してないのに参照されていることになってる
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。