Home > Net >  How to solve invalid character in identifier error
How to solve invalid character in identifier error

Time:12-20

So I am making a discord bot , I saw this code on a stackoverflow question

from datetime import datetime, timedelta    
now = datetime.now() # a datetime.datetime objekt 
last_claim_stamp = str(now.timestamp()) # save this into json
​last_claim=datetime.fromtimestamp(float(last_claim_stamp))

delta = now - last_claim # the timedelta between now and the last claim
​if delta > timedelta(hours=48): # if last claim is older than 48h; 24h until he can re use the command   24h time to claim his daily money again = 48h
   ​streak = 1 # reset the streak
else:
   ​streak  = 1          

@client.command()
@commands.check(user)
@commands.cooldown(1, 86400, commands.BucketType.user)
async def daily(ctx):
   ​with open("streak.json", "r") as f:
       ​data = json.load(f)
   ​streak = data[f"{ctx.author.id}"]["streak"]
   ​last_claim_stamp = data[f"{ctx.author.id}"]["last_claim"]
   ​last_claim = datetime.fromtimestamp(float(last_claim_stamp)
   ​now = datetime.now()
   ​delta = now - last_claim
   ​if delta > timedelta(hours=48):
       ​print("reset streak")
       ​streak = 1
   ​else:
       ​print("increase streak")
       ​streak  = 1
   ​daily = 45   (streak * 5)
   ​amount_after = data[f"{ctx.author.id}"]["balance"]   daily
   ​data[f"{ctx.author.id}"]["streak"] = streak
   ​data[f"{ctx.author.id}"]["balance"]  = daily
   ​data[f"{ctx.author.id}"]["last_claim"] = str(now.timestamp())
   ​with open("streak.json", "w") as f:
       ​json.dump(data, f, indent=2)
   ​embed = discord.Embed(title="Daily", colour=random.randint(0, 0xffffff), description=f"You've claimed your daily of **{daily}**, now you have **${amount_after}**")
   ​embed.set_footer(text=f"Your daily streak: {streak}")
   ​await ctx.send(embed=embed)


I got the error here -

File "main.py", line 1148
    ​last_claim=datetime.fromtimestamp(float(last_claim_stamp))
              ^
SyntaxError: invalid character in identifier

I retyped code the starting part where is the error but still no success please help.I used another interpreter , it showed the same error but at a different place i.e. (last_claim_stamp)

CodePudding user response:

Your code has an invisible Unicode character at the beginning of the name last_claim, specifically U 200B ZERO WIDTH SPACE

Indeed, the whole section of code seems to have U 200B characters at the beginning of most lines.

How to resolve this depends more on your editor / IDE than Python; you will need to figure out how to do a search and replace to remove them all, and possibly also some method of viewing them (although if it starts working, you can probably call it done). Depending on how they got there, you may also need to avoid using the same method in the future, or change settings so the U 200B characters don't get generated.

  • Related