我的代碼有問題。它是我的 discored bot 命令的一部分,但它無法在檔案中寫入任何內容而沒有錯誤
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()
感謝您的幫助
uj5u.com熱心網友回復:
您的代碼包含應該引發錯誤的陳述句。以下是其中至少一些:
str(i)應更換為i = str(i). 如果沒有這個,腳本會嘗試將i數字寫入檔案并拋出TypeError: write() argument must be str, not intint(a)a = int(a)閱讀檔案時應替換為。否則TypeError: can only concatenate str (not "int") to str嘗試時會拋出i = a i
如果您沒有收到任何錯誤,請確保此代碼完全運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514426.html
