#๐Ÿ”’ im doing a discord bot that randomize some values and return a value

55 messages ยท Page 1 of 1 (latest)

primal leaf
#

first of all, the values are in portugues, so if you dont understand the ' Seis olhos ' just think it as a random word that i want to be printed in discord, how do i set a bot to take a command and send my random code back to the user

small wadiBOT
#

@primal leaf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

primal leaf
somber owl
#

!d elif

small wadiBOT
#

8.1. The if statement

The if statement is used for conditional execution:


if_stmt ::= "if" assignment_expression ":" suite
            ("elif" assignment_expression ":" suite)*
            ["else" ":" suite]
```...
viscid bluff
#

๐Ÿ‘

#

@somber owl hey.๐Ÿ‘‹

somber owl
# primal leaf first of all, the values are in portugues, so if you dont understand the ' Seis ...

a quick way to do this using elif -

if x == 1:
   print(...)
elif x == 2:
   print(...)
elif x == 3:
   print(...)
...
```however, this is a bit tedious and repetitive

you could use a dictionary to map out each number to a corresponding string/text -
```py
mydict = {
   1: "Seis olhos",
   2: "Ilimitado",
   3: "Santuraio",
   ...
}

print( mydict.get(x, "Not Found!") )
```this simplifies things a lot!

but since it's still a bit repetitive, having the keys go from `1` to `10`.
you could still do a bit better!

it can be compressed and simplified even further.
utilizing `enumerate()` to count from 1; and `str.split()` to separate the data
#
mytext = """
Seis olhos
Ilimotado
Santuario
Transfiguraรงฤo
...
"""

data = mytext.split("\n")  # .splitlines()
```OR
```py
data = ["Seis olhos", "Ilimitado", ...]

then, simply index it. though it might confuse you with all that. so I'll stick to a dictionary.

mydict = dict( enumerate(data, start=1) )

print(mydict)
print( mydict.get(x, "Not Found!") )
```.
#

@primal leaf well, I'm jumping the gun a bit.

Have you learned about lists and dictionaries?

viscid bluff
#

@somber owl

somber owl
viscid bluff
kindred lintel
#

you should prolly get the basics of python down first then go to discord bots

median scaffold
rancid zephyr
primal leaf
#

just making the code to run the randrange in discord now

#

๐Ÿ™

#

i leaked my token ๐Ÿ’€

worthy osprey
#

Hey!

#

Your idea is great

#

You're using randrange to pick a random power/skill.

#

But I think your current code has a few syntax issues that are totally normal when you're starting out.

primal leaf
worthy osprey
#

Sure!

#

I've reviewd your code and I appreciate your creativity.

#

To make it the best it can be, I cleaned up the syntax and made it ready to run inside a Discord bot command

agile quest
#

one of which comes down to like what random mentioned above. there are more efficient and smaller ways to accomplish this but it would be of great value to learn the core concepts associated with them instead of just tossing in code you are given

#

especially since lists and dicts are very base level types that you will use a ton

primal leaf
agile quest
#

There are a lot of good info in the resources note on this server for good sources

#

!resources

small wadiBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

worthy osprey
#

I am willing to share the source code updated if you are interested in

primal leaf
worthy osprey
#

Okay

#

Just to clarify, I think you want a Discord bot that generates a random value from predefined list of Portuguese phrases

#

Am I right?

primal leaf
#

yeah

worthy osprey
#

Okay, I will send you the updated code.

small wadiBOT
#

random.choice(seq)```
Return a random element from the non-empty sequence *seq*. If *seq* is empty, raises [`IndexError`](https://docs.python.org/3/library/exceptions.html#IndexError).
worthy osprey
#

I think it works well on your end

kindred lintel
#

and it doesn't seem like you installed discord

worthy osprey
#

I think you're probably right

rancid zephyr
# primal leaf

That's better also u can use a disctionary and simplify this down to like 6 ish line

#

Oh nvm u did

#

Nice

small wadiBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.