8 Ball
Last updated
Last updated
You will be learning to use arguments and random choices to make an 8 ball command.
Before we can start coding this command, we need to import another library. Head to the top of your code, and under your last import, type import random
. So your import list should look like the following:
This library will allow us to pick a random item from a list, basically allowing us to pick a random response. This is crucial to making our eightball command.
We will be following the skeleton we used for the previous command. We just need to change a couple of things to allow us to take in arguments, or information from the sender of the command.
An example of an argument would be in bold in the command "!eightball will i win the lottery?". An argument can be a single word, or an entire string. For this case, we will be using an entire string as that is our question.
Now we need to modify our previous template. In async def eightball(message):
you will need to add a couple more parameters to eightball
after message. This will be *
and arg
. The *
tells the bot that all words following the command, !eightball
will belong to the variable called arg
, which we define right after it.
Because we defined arg as the argument, we can now reference arg to get all of the words following the command !eightball
. So for example, if I typed !eightball will i win the lottery?
, if I referenced arg, it would return will i win the lottery?
.
The modified template is as follows:
Now we need to create a list of responses that the eightball can give.
We can create a variable called responses, as they can also store lists. Lists in python are the same as if you were making a list for things you need to buy. How you write them is different, but they do the same thing, grouping a bunch of items together.
To create a list, like stated, create a variable, in this case, lets call it responses. After the equals sign, we can tell python we are creating a list by typing []. Anything in between these brackets will be considered part of the list. We can separate items (strings, numbers, etc) in the list with a comma. For example
would be an example of a list of strings. You can also split them up into different lines to make it easier to read. It is best practice to only do this when you have a long list, as short lists can be all contained in a single line.
Practice making lists by creating a list of eightball responses. Additionally, you can throw in a couple of your own responses if you'd like.
Now we need to pick a random response and assign it to a variable. Remember how we imported the random library earlier in this section, this is why. We can get a random choice from a list by using random.choice(list).
For example, to pick a random name you can use:
Now use this to pick a random response from the list of responses you created earlier.
Now we need to send a response back to the user. We can do this the same we did for our hello command using await message.channel.send(response). Create your response and add it to your code.
Remember the arg variable can be used to refer to the question.
Remember how we added non literal text strings from the previous section. Look back if you need to.
You can use multiple await message.channel.send(response)
in the same command.
If you want to split a message into 2 lines, use \n where you want it to split. For example "hello \nworld" would return:
"hello world"
Only select the Final Code tab once you have completed your eightball command. If you are stuck please see a mentor.
Your code should look something like the following at this point in the sushi card:
Only select the Final Code tab once you have completed your eightball command. If you are stuck please see a mentor.
If your code is not like this or is not working, please either read through the sushi card again OR come talk to one of the mentors who will guide you through the issue.