如何将生成的信息从python写入文件?

时间:2015-11-04 16:32:27

标签: python python-2.7

我想知道我是否可以将每个字符的属性信息写入文本文件中,我怎么能这样做?每当有人玩游戏时,都需要更新记事本文件中的属性。

def strength():
    dice12 = random.randint(1,12)
    dice04 = random.randint(1,4)
    strfinalstats = dice12/dice04+10
    print ">",strfinalstats,"strength"

def skill():
    dice12 = random.randint(1,12)
    dice04 = random.randint(1,4)
    skfinalstats = dice12/dice04+10
    print ">",skfinalstats,"skill!"

def player1():
    name1=raw_input("what would you like to call the first player?")
    print name1,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

def player2():
    name2=raw_input("what would you like to call the first player?")
    print name2,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

def player3():
    name3=raw_input("what would you like to call the first player?")
    print name3,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

1 个答案:

答案 0 :(得分:0)

首先你需要:``

f = open('filename', 'r+')

您可以放在每个属性的正下方:

f.write(attributes)

每当有人玩游戏时,记事本文件上的属性都会更新。

P / s:为什么你没有取消课程名称"播放器"然后创建三个实例?