我在试着为石头剪刀游戏创造一个胜利的条件。我试过这样做,但它就是坏了,完全不起作用。下面是一些需要改进的代码:
import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather
@client.command()
async def rps(ctx, user_choice):
rpsGame = ['rock', 'paper', 'scissors']
if user_choice == 'rock' or user_choice == 'paper' or user_choice == 'scissors':
await ctx.send(f'Choice: `{user_choice}`\nBot Choice: `{random.choice(rpsGame)}`')
bot_choice = f'{random.choice(rpsGame)}'
else:
await ctx.send('**Error** This command only works with rock, paper, or scissors.')
# Rock Win Conditions #
if user_choice == 'rock' and bot_choice == 'paper':
await ctx.send('I won!')
if user_choice == 'rock' and bot_choice == 'scissors':
await ctx.send('You won!')
# Paper Win Conditions #
if user_choice == 'paper' and bot_choice == 'rock':
await ctx.send('You won!')
if user_choice == 'paper' and bot_choice == 'scissors':
await ctx.send('I won!')
# Scissor Win Conditions #
if user_choice == 'scissors' and bot_choice == 'paper':
await ctx.send('You won!')
if user_choice == 'scissors' and bot_choice == 'rock':
await ctx.send('I won!')
client.run('token')
发布于 2020-10-20 05:13:14
这是对代码的重写,我修复了一些无用的f字符串,并将赢/输条件的缩进移到了if user_choice
is in rpsGame中,以防止variable referenced before assignment
@client.command()
async def rps(ctx, user_choice):
rpsGame = ['rock', 'paper', 'scissors']
if user_choice.lower() in rpsGame: # Better use this, its easier. [lower to prevent the bot from checking a word like this "rOcK or pApeR"
bot_choice = random.choice(rpsGame)
await ctx.send(f'Choice: `{user_choice}`\nBot Choice: `{bot_choice}`')
user_choice = user_choice.lower() # Also prevent a random word such as "rOcK"
if user_choice == bot_choice:
await ctx.send('We tied')
# Rock Win Conditions #
if user_choice == 'rock' and bot_choice == 'paper':
await ctx.send('I won!')
if user_choice == 'rock' and bot_choice == 'scissors':
await ctx.send('You won!')
# Paper Win Conditions #
if user_choice == 'paper' and bot_choice == 'rock':
await ctx.send('You won!')
if user_choice == 'paper' and bot_choice == 'scissors':
await ctx.send('I won!')
# Scissor Win Conditions #
if user_choice == 'scissors' and bot_choice == 'paper':
await ctx.send('You won!')
if user_choice == 'scissors' and bot_choice == 'rock':
await ctx.send('I won!')
else:
await ctx.send('**Error** This command only works with rock, paper, or scissors.')
您应该将bot_choice设置为1变量,这是唯一会破坏它的地方,因为机器人会发送一个与ctx.send
下的bot_choice不同的选择,我还添加了tied选项。
https://stackoverflow.com/questions/64437102
复制相似问题