第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

不和諧機(jī)器人 - Python 3 - 將用戶變量傳遞給其他函數(shù)

不和諧機(jī)器人 - Python 3 - 將用戶變量傳遞給其他函數(shù)

冉冉說 2022-09-27 10:43:26
我正在制作一個(gè)擲骰子不和諧機(jī)器人qith Python 3。骰子編號(hào)在字典中查找短語并輸出它。我使用六面骰子時(shí)做得很好,但我希望用戶能夠?qū)⑵湓O(shè)置為10面骰子。要擲骰子,將其設(shè)置為10面骰子是“!y1”,它是“!y d10”。默認(rèn)情況下,我將其設(shè)置為d6,但是當(dāng)我現(xiàn)在嘗試滾動(dòng)時(shí),它會(huì)給我一個(gè)變量未定義的錯(cuò)誤。有人可以幫我指出我錯(cuò)過了什么嗎?import osfrom random import randintimport discordfrom dotenv import load_dotenvload_dotenv()TOKEN = os.getenv('DISCORD_TOKEN')client = discord.Client()def ybna6_faces(x):    return{        1: 'Yes, And...',        2: 'Yes...',        3: 'Yes, But...',        4: 'No, But...',        5: 'No...',        6: 'No, And...'        }[x]def danger6_faces(x):        return{                1: 'Blank',                2: 'Blank',                3: 'Blank',                4: 'Blank',                5: 'Skull!',                6: 'Two Skull!!'                }[x]def ybna10_faces(x):    return{        1: 'Yes, And...',        2: 'Yes...',        3: 'Yes, But...',        4: 'No, But...',        5: 'No...',        6: 'No, And...',        7: '',        8: '',        9: '',        10: ''        }[x]def danger10_faces(x):        return{                1: 'Blank',                2: 'Blank',                3: 'Blank',                4: 'Blank',                5: 'Skull!',                6: 'Two Skull!!',                7: '',                8: '',                9: '',                10: ''                }[x]sides = 6#active_ybna = 6#active_danger = 6w=""#Successful server connection message@client.eventasync def on_ready():        print(f'{client.user.name} has connected to Discord!')
查看完整描述

2 回答

?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

您有兩個(gè)語句的計(jì)算結(jié)果都可以為 ,這意味著變量可能永遠(yuǎn)不會(huì)被創(chuàng)建。ifFalsesides


這是因?yàn)槟鷦?chuàng)建的 在函數(shù)之外,使其超出范圍。您需要將其移動(dòng)到函數(shù)內(nèi)部或聲明為全局(不建議)。sidessides


...


#active_ybna = 6

#active_danger = 6


w=""


#Successful server connection message

@client.event

async def on_ready():

    print(f'{client.user.name} has connected to Discord!')


#Makes sure bot doesn't respond to itself

@client.event

async def on_message(message):

    sides = 6

    if message.author == client.user:

        return


    #Choose die (6 or 10)

    if message.content.startswith('!y d6'):

        sides = 6


    if message.content.startswith('!y d10'):

        sides = 10


    #Evaluate rolls

    if message.content.startswith('!y'):

        if int(message.content[2])>int(0):

            z = int(message.content[2])

            for x in range(z):   

                 y = randint(1,int(sides))

                 active_ybna = 'ybna'+ sides + '_faces()'

                 response = 'Roll ' + str(x+1) + ': ' + active_ybna(y)

                 await message.channel.send(response)

         if len(message.content)>=4:

             if int(message.content[4])>int(0):

                 z = int(message.content[4])

                 for x in range(z):

                     y=randint(1,int(sides))

                     active_danger = 'danger'+ sides + '_faces()'

                     response = 'Danger ' + str(x+1) + ': ' + active_danger(y)

                     await message.channel.send(response)


client.run(TOKEN)您有兩個(gè)語句的計(jì)算結(jié)果都可以為 ,這意味著變量可能永遠(yuǎn)不會(huì)被創(chuàng)建。ifFalsesides


這是因?yàn)槟鷦?chuàng)建的 在函數(shù)之外,使其超出范圍。您需要將其移動(dòng)到函數(shù)內(nèi)部或聲明為全局(不建議)。sidessides


...


#active_ybna = 6

#active_danger = 6


w=""


#Successful server connection message

@client.event

async def on_ready():

    print(f'{client.user.name} has connected to Discord!')


#Makes sure bot doesn't respond to itself

@client.event

async def on_message(message):

    sides = 6

    if message.author == client.user:

        return


    #Choose die (6 or 10)

    if message.content.startswith('!y d6'):

        sides = 6


    if message.content.startswith('!y d10'):

        sides = 10


    #Evaluate rolls

    if message.content.startswith('!y'):

        if int(message.content[2])>int(0):

            z = int(message.content[2])

            for x in range(z):   

                 y = randint(1,int(sides))

                 active_ybna = 'ybna'+ sides + '_faces()'

                 response = 'Roll ' + str(x+1) + ': ' + active_ybna(y)

                 await message.channel.send(response)

         if len(message.content)>=4:

             if int(message.content[4])>int(0):

                 z = int(message.content[4])

                 for x in range(z):

                     y=randint(1,int(sides))

                     active_danger = 'danger'+ sides + '_faces()'

                     response = 'Danger ' + str(x+1) + ': ' + active_danger(y)

                     await message.channel.send(response)


client.run(TOKEN)


查看完整回答
反對(duì) 回復(fù) 2022-09-27
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

我認(rèn)為我發(fā)布的變量未聲明為全局變量。我稍微簡(jiǎn)化了這一切,并將其塞進(jìn)一個(gè)具有多個(gè)IF的事件中。


還添加了一些其他代碼...


# ybnabot.py

#d6 functionality


import os

from random import randint


import discord

from dotenv import load_dotenv


load_dotenv()

TOKEN = os.getenv('DISCORD_TOKEN')


client = discord.Client()


def ybna6_faces(x):

    return{

        1: 'No, And...',

        2: 'No...',

        3: 'No, But...',

        4: 'Yes, But...',

        5: 'Yes...',

        6: 'Yes, And...'

        }[x]


def danger6_faces(x):

        return{

        1: 'Blank',

        2: 'Blank',

        3: 'Blank',

        4: 'Blank',

        5: ':skull:',

        6: ':skull: :skull:'

        }[x]


def ybna10_faces(x):

    return{

        1: 'No, And...',

        2: 'No, And, But...',

        3: 'No...',

        4: 'Not, But...',

        5: 'No, But, And...',

        6: 'Yes, But, And...',

        7: 'Yes, But...',

        8: 'Yes...',

        9: 'Yes, And, But...',

        10: 'Yes, And...'

        }[x]


def danger10_faces(x):

        return{

                1: 'Blank',

                2: 'Blank',

                3: 'Blank',

                4: 'Blank',

                5: ':skull',

                6: ':skull: :skull',

                7: 'Blank',

                8: 'Blank',

                9: ':skull:',

                10: ':skull: :skull:'

                }[x]


@client.event

async def on_ready():

    global sides

    sides = 6

    print(f'{client.user.name} has connected to Discord!')



@client.event

async def on_message(message):

    global sides

    if message.author == client.user:

        return


#Send Help DM

    if message.content.startswith('!y help'):

        await message.author.send('YBNA Bot Help!')

        await message.author.send('Commands:')

        await message.author.send('!y X Z - Roll X number of YBNA dice and Z Danger dice (Danger die can be omitted)')

        await message.author.send('!y d6 - Choose six sided dice')

        await message.author.send('!y d10 - Choose 10 sided dice')

        await message.author.send('!y dice - Displays which dice are selected (d6 or d10)')

        await message.author.send('!y help - This message!')

        await message.author.send('txutfz73 credits here')

        await message.author.send('github link here')

        return



#Check die used

    if message.content.startswith('!y dice'):

        response = 'Using ' + str(sides) + ' sided dice!'

        await message.channel.send(response)

        return


#Choose d6 or d10

    if message.content.startswith('!y d6'):

        sides = 6

        await message.channel.send('Now using d6!')

        return


    if message.content.startswith('!y d10'):

        sides = 10

        response = 'Now using d10!'

        await message.channel.send(response)

        return





#Roll d6

    if sides == 6:

        if message.content.startswith('!y'):

            if int(message.content[2])>int(0):

                z = int(message.content[2])

                for x in range(z):

                    y = randint(1,6)

                    response = 'Roll ' + str(x+1) + ': ' + ybna6_faces(y) + str(sides)

                    await message.channel.send(response)


        if len(message.content)>=4:

            if int(message.content[4])>int(0):

                z = int(message.content[4])

                for x in range(z):

                    y=randint(1,6)

                    response = 'Danger ' + str(x+1) + ': ' + danger6_faces(y)

                    await message.channel.send(response)


#Roll d10

    else:

        if message.content.startswith('!y'):

            if int(message.content[2])>int(0):

                z = int(message.content[2])

                for x in range(z):

                    y = randint(1,10)

                    response = 'Roll ' + str(x+1) + ': ' + ybna10_faces(y) + str(sides)

                    await message.channel.send(response)


        if len(message.content)>=4:

            if int(message.content[4])>int(0):

                z = int(message.content[4])

                for x in range(z):

                    y=randint(1,10)

                    response = 'Danger ' + str(x+1) + ': ' + danger10_faces(y)

                    await message.channel.send(response)



client.run(TOKEN)



查看完整回答
反對(duì) 回復(fù) 2022-09-27
  • 2 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)