'discord.py - Is there a way to fix this invalid syntax error for picking a random date
so basically what I'm trying to do with discord.py is create a command that basically gives you a random date of 2022-01-01, to 2023-01-01. I think I have good code, but I get a invalid syntax error whenever I try running it. Here is my code.
import datetime
import random
from discord.ext import commands
@client.command(name='death')
async def death(ctx, *, user_that_dies:discord.User):
await ctx.send(f'**PERSON**\n\n{user_that_dies:discord.User}\n\n**WHEN THEY ARE GOING TO DIE**\n\n{radar.random_datetime(start='2022-01-01', stop='2023-01-01')
}')
Solution 1:[1]
await ctx.send(f'**PERSON**\n\n{user_that_dies:discord.User}\n\n**WHEN THEY ARE GOING TO DIE**\n\n{radar.random_datetime(start='2022-01-01', stop='2023-01-01')}')
This is invalid syntax because you're nesting single quotes inside another single quoted string, which you can't do. Use double-quotes for the inner or outer quotes.
Solution 2:[2]
random.random_datetime isn't an actual function.
You can generate a random datetime in the range you want with something like this
import random
from datetime import date, timedelta
def random_datetime():
return date(2022, 1, 1) + timedelta(days=random.randint(0, 365))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Gordon |
| Solution 2 | Brendan Abel |
