I was making a discord bot with the help of this [video][1]. My other code runs well but whenever I delete a encouragement I face the problem. The problem shows in this index = int(msg.split("$del",1)[1])
line. The console tells me that there’s a literal for int() with base 10 problem. And there for I can’t delete any encouragements. But I can add them or do other things. Here’s some of the code of my program:
def delete_encouragment(index):
encouragements = db["encouragements"]
if len(encouragements) > index:
del encouragements[index]
db["encouragements"] = encouragements
if msg.startswith("$del"):
encouragements = []
if "encouragements" in db.keys():
index = int(msg.split("$del",1)[1])
delete_encouragment(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
The problem is in the number 4 line of 2nd pera.
CodePudding user response:
You must convert It ti float before of converting it to int. Here's the correct line:
index = int(float(msg.split("$del",1)[1]))
CodePudding user response:
The line failing is:
index = int(msg.split("$del",1)[1])
The split is working fine, as is the index, so it must be that the 1th element of msg.split("$del", 1)
cannot be cast to an integer (perhaps it contains a .?).
If you add a print(msg)
line directly above and post the output in your question, I'll update this answer with a working split and/or cast.