所以我目前正在尝试编写一个简单的基于文本的游戏,并且我有一个基于文件的保存系统。我最近添加了交叉文件窃取(不要问),我测试了它。几次保存后,我注意到系统设计为只允许在从保存中加载被盗内容之间进行一次盗窃,这已停止工作。我查看了我的保存文件并看到了 这个:
又过了一会儿,这发生在另一个 保存文件上:
现在出现了代码删除保存文件内容的问题!谁能解释为什么要添加额外的字符?询问您是否需要更多代码,这只是 save 和 rob 功能。
代码:
import os
import time
import random as r
def savefile(data, slot):
if(len(data)>6):
savef=open("savefile"+str(slot)+".txt", 'w')
print(data[6])
data[6][0] = data[6][0].replace("'", '')
data[6][2] = data[6][2].replace("'", '')
print(data[6])
if(len(data[5])==2):
data[5].append(0)
savef.write(str(data[0])+"\n"+str(data[1])+"\n"+str(data[2]).replace(" ", "")+"\n"+str(data[3])+"\n"+str(data[4])+"\n"+str(data[5])+"\n"+str(data[6]))
savef.close()
else:
print("the save is outdated")
def localrob(data):
#UNFINISHED
print("Choose your target(enter their ID)")
savenum=1
data[1]=int(data[1])
while True:
try:
save=open("savefile"+str(savenum)+".txt", "r")
name=save.read().split()[0]
print("["+str(savenum)+"] "+name)
savenum=savenum+1
save.close()
except:
break
try:
target=int(input())
except:
print("wow thats not a number idiot")
return data
if(target==slotr):
print("You cant rob yourself silly!")
return data
else:
targetfile=open("savefile"+str(target)+".txt")
targetdata=targetfile.read().split()
if(targetdata[6][0]=='False'):
pass
else:
print("This mans already been stolen from! Give em a rest!")
targetfile.close()
return data
targetdata[1]=int(targetdata[1])
bounty=r.randint(int(targetdata[1]/5), int(targetdata[1]/2))
spend=r.randint(int(targetdata[1]/15), int(targetdata[1]/5))
ques=input(targetdata[0]+" has " + str(bounty) + " coins up for grabs, but if you get lucky, you might be able to snag more. It will cost "+str(spend)+" coins. Do you go through? (y/n)")
if ques.lower()=='y':
extraspent=int(input("How much extra do you want to spend?"))
if(int(data[1])-extraspent-spend<0):
print("lol ur too poor to rob them")
return data
chances=int(300*(spend+extraspent)/bounty)-int(data[5][2])
if(chances>100):
chances=100
chances=chances-10
ohmy=r.randint(0, 100)
if(data[5][1]):
last=input("Your chances are "+str(chances)+"%. Are you sure you want to attack? (y/n)")
if(last=='y'):
pass
else:
print("Ok.")
return rob(data)
print("You invested %s coins into this, it better go well." % (str(extraspent+spend)))
print("Stealing all of "+targetdata[0]+" possesions...")
time.sleep(r.randint(1, 10))
print(targetdata[6])
if ohmy==1:
print("Even though you didnt think so, you were able to get BASICACLLY EVERYTHING LMAO. You earn %s coins(after paying off your investment" % (str(targetdata[1])))
data[1]=int(data[1])+targetdata[1]
targetdata[6]=[True, data[0], targetdata[1]]
targetdata[1]=0
elif ohmy<5:
print("You were able to steal twice what you think what you could.")
data[1]=int(data[1])+bounty*2-spend-extraspent
targetdata[1]=targetdata[1]-bounty*2
targetdata[6]=[True, data[0], bounty*2]
elif ohmy<chances:
print("You successfully stole %s from this poor man, getting a net profit of %s coins." % (str(bounty), str(bounty-extraspent-spend)))
data[1]=int(data[1])+bounty-extraspent-spend
targetdata[1]=targetdata[1]-bounty
targetdata[6]=[True, data[0], bounty]
elif ohmy<90:
print("Oof, you failed and lost your investment.")
data[1]=int(data[1])-extraspent-spend
targetdata[6]=[False, data[0], 0]
else:
print("WOW, you got caught! You paid twice your investment.")
data[1]=data[1]-2*extraspent-2*spend
targetdata[6]=[False, data[0], 0]
savefile(targetdata, target)
targetfile.close()
return data
return data
#UNFINISHED