#๐ im doing a discord bot that randomize some values and return a value
55 messages ยท Page 1 of 1 (latest)
@primal leaf
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.
sorry if the code is messy, im learning python
look into "guard-if", elif and dictionaries. this could help reduce a lot of the indentation and code in general.
!d elif
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?
@somber owl
?
Do you know of OLL
no, not at all
you should prolly get the basics of python down first then go to discord bots
not in help posts fyi
That code almost gave me a stroke please I beg u just use elif
sorry, this is literally my first code ๐
just making the code to run the randrange in discord now
๐
i leaked my token ๐
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.
like? i want the code to be the best as i can make it to ne! any hint is more than welcome
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
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
i will see about that too, im doing some videos in youtube from "curso em video", idk about the quality of the videos
There are a lot of good info in the resources note on this server for good sources
!resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
I am willing to share the source code updated if you are interested in
sure im, can you send here for me to analyze it
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?
yeah
Okay, I will send you the updated code.
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).
pls do not sync in on_ready
and it doesn't seem like you installed discord
I think you're probably right
That's better also u can use a disctionary and simplify this down to like 6 ish line
Oh nvm u did
Nice
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.