Home > Software engineering >  Python code does not write anything inside the file
Python code does not write anything inside the file

Time:10-14

I have and problem with my code. It is a part of mine command for discored bot, but it fails to write anything inside file with no error

item = "Scrap Metal"
price = 0.56 #per gram
cost = grams * price
cost = round(cost)
human = random.randrange(1, 3)
if human == 1:
    human = "Chinese web store"
elif human == 2:
    human = "TechKnow scrap yard"
elif human == 3:
    human= "Random guy on the street"
embed = discord.Embed(title="You went mining!", description=f"You have found **{grams}g** of **{item}**.\n\nYou sold it to **{human}** for **{cost}**!", color=0x00ffee)
await ctx.channel.send(embed=embed)
try:
    f = open(f"./all/users/{ctx.author.id}/balance.txt", "r ")
    a = f.read()
    int(a)
    f.close()
except:
    a = 0

i = cost
i = a   i
str(i)

f = open(f"./all/users/{ctx.author.id}/balance.txt", "w ")
f.seek(0)
f.write(i)
f.close()

Thank you for your help upfront

CodePudding user response:

Your code contains statements that should throw an errors. Here are at least some of them:

  1. str(i) should be replaced to i = str(i). Without this, the script tries to write i as a number to the file and throws TypeError: write() argument must be str, not int
  2. int(a) should be replaced by a = int(a) when you read the file. Otherwise TypeError: can only concatenate str (not "int") to str will be thrown when you try i = a i

If you don't get any errors, then make sure that this code runs at all.

  • Related