2015-11-04 89 views
-2

我想知道我是否可以將每個字符的屬性信息寫入文本文件,我將如何能夠做到這一點?每次有人玩遊戲時,記事本文件上的屬性都需要更新。如何將我從python生成的信息寫入文件?

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

你有沒有嘗試過任何東西? – jonrsharpe

+0

有很多「如何將X寫入文件?」關於SO Python的問題。請搜索其中的一個,並遵循適當的答案。 –

+0

[Python保存到文件]的可能重複(http://stackoverflow.com/questions/9536714/python-save-to-file) –

回答

0

首先,你需要:``

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

你可以把正確的每個屬性如下:

f.write(attributes) 

的屬性在記事本文件都會更新每個人玩遊戲的時間。

P/s:你爲什麼不刪除類名「player」然後創建三個實例?

相關問題