Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python code does not write anything inside the file

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

  1. str(i) should be replaced to i = 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading