撰寫大家熟知的終極密碼
- 示意圖
開始撰寫
# cogs/guess.py
@commands.command()
async def guess(self, ctx):
# 檢查回傳的是否是同一個人(已及是否在同一個頻道)
def check(number):
return number.author == ctx.author and number.channel == ctx.message.channel
global lowernumber
global highernumber
lowernumber = 1
highernumber = 100
number = random.randint(lowernumber, highernumber)
# print(number)
await ctx.send('1-100,任意選一個數字')
for i in range(0, 5):
response = await self.bot.wait_for('message', check = check)
try :
guess = int(response.content)
except:
await ctx.send("請輸入數字")
if guess == number :
await ctx.send("猜對了")
break
if guess > 100 :
await ctx.send("超過100,格式錯誤")
if guess < number:
lowernumber = guess
await ctx.send(f"比 {lowernumber}大,比 {highernumber} 小")
if guess > number :
highernumber = guess
await ctx.send(f"比 {lowernumber}大,比 {highernumber} 小")
def setup(bot):
bot.add_cog(guess(bot))
async def guess(self, ctx, ante):
pass
# 賭注
HM = [2.8, 2.3, 1.9, 1.5, 1.1]
細部的部分
if int(ante) > 240 :
await ctx.send("賭注不得高於240")
return
ante = int(ante)
# 省略
if guess == number :
await open_account(ctx.author)
bal = await update_bank(ctx.author)
final = int(ante) * float(HM[i])
await update_bank(ctx.author, int(final))
await ctx.send(f"猜對了,你獲得了{final}$$")
break
- 結果示意圖